home *** CD-ROM | disk | FTP | other *** search
/ Almathera Ten Pack 3: CDPD 3 / Almathera Ten on Ten - Disc 3: CDPD3.iso / ab20 / ab20_archive / utilities / printer / post-1.6src.lzh / post.c < prev    next >
C/C++ Source or Header  |  1991-04-17  |  76KB  |  2,447 lines

  1. /* PostScript interpreter file "post.c" - the main program (Amiga)
  2.  * (C) Adrian Aylward 1989, 1991
  3.  *
  4.  * This file contains the code to parse the command line arguments, the
  5.  * WorkBench and intuition interface, and the printer screen or IFF file]
  6.  * output routines.  It is therfore heavily Amiga dependent.  It calls the
  7.  * interpreter library for all the PostScript stuff.
  8.  *
  9.  * There are also various Lattice dependencies: the startup code in _main,
  10.  * the menu task, the flush and copy functions, and the floating point trap
  11.  * and break handling.
  12.  */
  13.  
  14. # include <arpbase.h>
  15. # include <dos.h>
  16. # include <devices/printer.h>
  17. # include <devices/prtbase.h>
  18. # include <exec/exec.h>
  19. # include <exec/execbase.h>
  20. # include <exec/tasks.h>
  21. # include <graphics/gfxbase.h>
  22. # include <graphics/text.h>
  23. # include <intuition/intuition.h>
  24. # include <libraries/dosextens.h>
  25. # include <workbench/icon.h>
  26. # include <workbench/startup.h>
  27. # include <workbench/workbench.h>
  28. # include <proto/diskfont.h>
  29. # include <proto/dos.h>
  30. # include <proto/exec.h>
  31. # include <proto/graphics.h>
  32. # include <proto/icon.h>
  33. # include <proto/intuition.h>
  34. # include <stdio.h>
  35. # include <string.h>
  36.  
  37. # include "postlib.h"
  38.  
  39. /* Assembler routines */
  40.  
  41. extern void insertbreak(void);
  42. extern void deletebreak(void);
  43. extern void insertftrap(void);
  44. extern void deleteftrap(void);
  45.  
  46. /* Routines defined and referenced only within this module */
  47.  
  48. extern void errmsg(char *string);
  49. extern void tidyup(void);
  50. extern void setprinter(void);
  51. extern void setprintden(void);
  52. extern void setreqgadgets(void);
  53. extern void getreqgadgets(void);
  54. extern void enablegadg(struct Gadget *gadget, int enable);
  55. extern void setgadgbool(struct Gadget *gadget, int value);
  56. extern int  getgadgbool(struct Gadget *gadget);
  57. extern void setgadgint(struct Gadget *gadget, int value);
  58. extern int  getgadgint(struct Gadget *gadget);
  59. extern int  getgadgabs(struct Gadget *gadget);
  60. extern int  strtoint(char **sp, int *ip);
  61. extern void sendmenu(int action);
  62. extern void __saveds menuproc(void);
  63. # ifndef STATICLINK
  64. extern void __saveds __asm flushpage(register __d0 int y1,
  65.                                      register __d1 int y2);
  66. extern void __saveds __asm copypage(register __d0 int num);
  67. # endif
  68. extern void printpage(void);
  69. extern void iffpage(void);
  70. extern int  igcd(int n, int m);
  71.  
  72. /* Message structure */
  73.  
  74. struct PSmessage
  75. {   struct Message ExecMessage;
  76.     long class;            /* Always zero */
  77.     short action;          /* Action - from Post to handler */
  78.     short command;         /* Command - from handler to Post */
  79.     struct BitMap *bitmap; /* The bitmap */
  80.     short y1, y2;          /* Min and max+1 y values to flush */
  81.     short result;          /* Result  (return code) */
  82.     short length;          /* Length of string */
  83.     char *string;          /* String */
  84.     long window;           /* Always zero */
  85.     short errnum;          /* Last error number */
  86.     short zero;            /* Reserved, presently always zero */
  87. };
  88.  
  89. /* Actions */
  90.  
  91. # define PSACTOPEN    1 /* Open */
  92. # define PSACTCLOSE   2 /* Close */
  93. # define PSACTFLUSH   3 /* Flush out the bitmap */
  94. # define PSACTPAUSE   4 /* Pause at the end of a page */
  95. # define PSACTCOMMAND 5 /* Get a command */
  96. # define PSACTEXIT    6 /* Exit */
  97.  
  98. /* Commands */
  99.  
  100. # define PSCOMQUIT    1 /* Quit */
  101. # define PSCOMRESTART 2 /* Restart */
  102. # define PSCOMFILEF   3 /* Load Font */
  103. # define PSCOMFILEL   4 /* Load File */
  104. # define PSCOMFILER   5 /* Run File */
  105. # define PSCOMINTER   6 /* Interactive */
  106.  
  107. /* Lattice startup */
  108.  
  109. extern struct WBStartup *WBenchMsg;
  110.  
  111. /* External data (initialised to zero) */
  112.  
  113. int retcode, errcode, ioerror;
  114. int fatalerror, restarterror, requested;
  115.  
  116. int arec;
  117.  
  118. BPTR errfh, confh;
  119.  
  120. # ifndef STATICLINK
  121. struct library *PSbase;
  122. # endif
  123. struct PSparm parm;
  124. struct PSmessage menumsg;
  125.  
  126. struct ArpBase *ArpBase;
  127. struct FileRequester *filereq, *filereq1, *filereq2;
  128.  
  129. struct Process *process;
  130. struct MsgPort *mainport, *menuport;
  131. struct Task *menutask;
  132. struct Screen *screen;
  133. struct Window *syswindow, *reqwindow, *errwindow, *bitwindow, *intwindow;
  134. struct BitMap bitmap;
  135. struct ColorMap colormap;
  136. struct TextFont *textfont;
  137.  
  138. int propen, prden;
  139. struct IODRPReq prreq;
  140. struct PrinterData *prdata;
  141. struct PrinterExtendedData *prextdata;
  142. struct Preferences *prprefs;
  143. struct RastPort prrast;
  144. struct MsgPort *prport;
  145. ULONG prsig;
  146. UBYTE prstatus[2];
  147.  
  148. int breakset, ftrapset;
  149.  
  150. int winxbase, winybase, winxsize, winysize, winxpos, winypos;
  151.  
  152. /* Colour tables.  The default color map type is a vector or UWORD RGB
  153.  * values.  The colours are inverted as we set a bit in the bitmap whenever
  154.  * we want to place a drop of (black or cyan/magenta/yellow) ink */
  155.  
  156. static UWORD bcolors[2] =  /* Black and white */
  157. {   0xfff, 0x000                /* White   black */
  158. };
  159.  
  160. static UWORD ccolors[16] = /* Colour (RGB or CMYK) */
  161. {   0xfff, 0x0ff, 0xf0f, 0x00f, /* White   cyan    magenta blue */
  162.     0xff0, 0x0f0, 0xf00, 0x000, /* Yellow  green   red     black */
  163.     0x000, 0x000, 0x000, 0x000, /* Black */
  164.     0x000, 0x000, 0x000, 0x000  /* Black */
  165. };
  166.  
  167. char titlewait[]    = POSTVER " Copyright Adrian Aylward 1989, 1991";
  168. char titlestart[]   = POSTVER " Running startup file(s)";
  169. char titlerunning[] = POSTVER " Running";
  170. char titleinter[]   = POSTVER " Interactive";
  171. char titlepaused[]  = POSTVER " Paused";
  172.  
  173. char hailfilef[]    = POSTVER " Select font to load";
  174. char hailfilen[]    = POSTVER " Select file to load";
  175. char hailfiles[]    = POSTVER " Select file to run";
  176.  
  177. /* The default font and screen */
  178.  
  179. struct TextAttr topaz11 =       /* Font is Topaz 11 */
  180. {   "topaz.font", 11, FS_NORMAL, FPF_DISKFONT };
  181.  
  182. struct NewScreen newscreen =    /* Screen */
  183. {   0, 0, 640, 512, 1, 0, 15, LACE|HIRES, CUSTOMSCREEN, &topaz11,
  184.     (UBYTE *) &titlewait, NULL, NULL
  185. };
  186.  
  187. /* Stuff for the options requestor */
  188.  
  189. # define GADGOK    1  /* Gadget id's */
  190. # define GADGCAN   2
  191. # define GADGPRI   3
  192. # define GADGBW    4
  193. # define GADGC3    5
  194. # define GADGC4    6
  195. # define GADGPD1   7
  196. # define GADGPD2   8
  197. # define GADGPD3   9
  198. # define GADGPD4  10
  199. # define GADGPD5  11
  200. # define GADGPD6  12
  201. # define GADGPD7  13
  202. # define GADGABS  14  /* Integer gadget, absolute value */
  203. # define GADGRST  15  /* Dummy gadget to restart after error */
  204.  
  205. char undobuff[80];
  206.  
  207. SHORT bordervec1[] =
  208. {   0, 0, 67, 0, 67, 14, 0, 14, 0, 1 };
  209. struct Border border1 =
  210. {   -2, -2, 15, 0, JAM1, 5, bordervec1, NULL };
  211.  
  212. UBYTE mlgadgbuff[8];
  213. struct StringInfo mlgadginfo =
  214. {   mlgadgbuff, (UBYTE *)undobuff, 0, 8, 0, 0, 0, 0, 0, 0, 0, 0, NULL };
  215. struct Gadget mlgadg =
  216. {   NULL,    500, 212, 64, 13, 0, STRINGRIGHT|LONGINT|RELVERIFY, STRGADGET,
  217.     (APTR)&border1, NULL, NULL, 0, (APTR)&mlgadginfo, GADGABS, NULL };
  218.  
  219. UBYTE mvgadgbuff[8];
  220. struct StringInfo mvgadginfo =
  221. {   mvgadgbuff, (UBYTE *)undobuff, 0, 8, 0, 0, 0, 0, 0, 0, 0, 0, NULL };
  222. struct Gadget mvgadg =
  223. {   &mlgadg, 500, 198, 64, 13, 0, STRINGRIGHT|LONGINT|RELVERIFY, STRGADGET,
  224.     (APTR)&border1, NULL, NULL, 0, (APTR)&mvgadginfo, GADGABS, NULL };
  225.  
  226. UBYTE mhgadgbuff[8];
  227. struct StringInfo mhgadginfo =
  228. {   mhgadgbuff, (UBYTE *)undobuff, 0, 8, 0, 0, 0, 0, 0, 0, 0, 0, NULL };
  229. struct Gadget mhgadg =
  230. {   &mvgadg, 500, 184, 64, 13, 0, STRINGRIGHT|LONGINT|RELVERIFY, STRGADGET,
  231.     (APTR)&border1, NULL, NULL, 0, (APTR)&mhgadginfo, GADGABS, NULL };
  232.  
  233. UBYTE mfgadgbuff[8];
  234. struct StringInfo mfgadginfo =
  235. {   mfgadgbuff, (UBYTE *)undobuff, 0, 8, 0, 0, 0, 0, 0, 0, 0, 0, NULL };
  236. struct Gadget mfgadg =
  237. {   &mhgadg, 500, 170, 64, 13, 0, STRINGRIGHT|LONGINT|RELVERIFY, STRGADGET,
  238.     (APTR)&border1, NULL, NULL, 0, (APTR)&mfgadginfo, GADGABS, NULL };
  239.  
  240. SHORT bordervec2[] =
  241. {   0, 0, 43, 0, 43, 14, 0, 14, 0, 1 };
  242. struct Border border2 =
  243. {   -2, -2, 15, 0, JAM1, 5, bordervec2, NULL };
  244.  
  245.  
  246. UBYTE ydgadgbuff[5];
  247. struct StringInfo ydgadginfo =
  248. {   ydgadgbuff, (UBYTE *)undobuff, 0, 5, 0, 0, 0, 0, 0, 0, 0, 0, NULL };
  249. struct Gadget ydgadg =
  250. {   &mfgadg, 388, 198, 40, 13, 0, STRINGRIGHT|LONGINT|RELVERIFY, STRGADGET,
  251.     (APTR)&border2, NULL, NULL, 0, (APTR)&ydgadginfo, GADGABS, NULL };
  252.  
  253. UBYTE xdgadgbuff[5];
  254. struct StringInfo xdgadginfo =
  255. {   xdgadgbuff, (UBYTE *)undobuff, 0, 5, 0, 0, 0, 0, 0, 0, 0, 0, NULL };
  256. struct Gadget xdgadg =
  257. {   &ydgadg, 345, 198, 40, 13, 0, STRINGRIGHT|LONGINT|RELVERIFY, STRGADGET,
  258.     (APTR)&border2, NULL, NULL, 0, (APTR)&xdgadginfo, GADGABS, NULL };
  259.  
  260. UBYTE yogadgbuff[5];
  261. struct StringInfo yogadginfo =
  262. {   yogadgbuff, (UBYTE *)undobuff, 0, 5, 0, 0, 0, 0, 0, 0, 0, 0, NULL };
  263. struct Gadget yogadg =
  264. {   &xdgadg, 388, 184, 40, 13, 0, STRINGRIGHT|LONGINT, STRGADGET,
  265.     (APTR)&border2, NULL, NULL, 0, (APTR)&yogadginfo, 0, NULL };
  266.  
  267. UBYTE xogadgbuff[5];
  268. struct StringInfo xogadginfo =
  269. {   xogadgbuff, (UBYTE *)undobuff, 0, 5, 0, 0, 0, 0, 0, 0, 0, 0, NULL };
  270. struct Gadget xogadg =
  271. {   &yogadg, 345, 184, 40, 13, 0, STRINGRIGHT|LONGINT, STRGADGET,
  272.     (APTR)&border2, NULL, NULL, 0, (APTR)&xogadginfo, 0, NULL };
  273.  
  274. UBYTE ygadgbuff[5];
  275. struct StringInfo ygadginfo =
  276. {   ygadgbuff, (UBYTE *)undobuff, 0, 5, 0, 0, 0, 0, 0, 0, 0, 0, NULL };
  277. struct Gadget ygadg =
  278. {   &xogadg, 388, 170, 40, 13, 0, STRINGRIGHT|LONGINT|RELVERIFY, STRGADGET,
  279.     (APTR)&border2, NULL, NULL, 0, (APTR)&ygadginfo, GADGABS, NULL };
  280.  
  281. UBYTE xgadgbuff[5];
  282. struct StringInfo xgadginfo =
  283. {   xgadgbuff, (UBYTE *)undobuff, 0, 5, 0, 0, 0, 0, 0, 0, 0, 0, NULL };
  284. struct Gadget xgadg =
  285. {   &ygadg, 345, 170, 40, 13, 0, STRINGRIGHT|LONGINT|RELVERIFY, STRGADGET,
  286.     (APTR)&border2, NULL, NULL, 0, (APTR)&xgadginfo, GADGABS, NULL };
  287.  
  288. short __chip idata1[] =
  289. {   0xfffe, 0x8002, 0x8002, 0x8002, 0x8002, 0x8002, 0x8002, 0x8002,
  290.     0x8002, 0x8002, 0x8002, 0x8002, 0x8002, 0x8002, 0xfffe,
  291.     0xfffe, 0x8002, 0x8002, 0x8002, 0x8002, 0x8002, 0x8002, 0x8002,
  292.     0x8002, 0x8002, 0x8002, 0x8002, 0x8002, 0x8002, 0xfffe,
  293.     0xfffe, 0x8002, 0x8002, 0x8002, 0x8002, 0x8002, 0x8002, 0x8002,
  294.     0x8002, 0x8002, 0x8002, 0x8002, 0x8002, 0x8002, 0xfffe,
  295.     0xfffe, 0x8002, 0x8002, 0x8002, 0x8002, 0x8002, 0x8002, 0x8002,
  296.     0x8002, 0x8002, 0x8002, 0x8002, 0x8002, 0x8002, 0xfffe };
  297. struct Image image1 =
  298. {   -2, -2, 15, 15, 4, &idata1[0], 15, 0, NULL };
  299.  
  300. struct IntuiText itext30 =
  301. {   15, 0, JAM2, 1, 1, NULL, "7", NULL };
  302. struct IntuiText itext29 =
  303. {   15, 0, JAM2, 1, 1, NULL, "6", NULL };
  304. struct IntuiText itext28 =
  305. {   15, 0, JAM2, 1, 1, NULL, "5", NULL };
  306. struct IntuiText itext27 =
  307. {   15, 0, JAM2, 1, 1, NULL, "4", NULL };
  308. struct IntuiText itext26 =
  309. {   15, 0, JAM2, 1, 1, NULL, "3", NULL };
  310. struct IntuiText itext25 =
  311. {   15, 0, JAM2, 1, 1, NULL, "2", NULL };
  312. struct IntuiText itext24 =
  313. {   15, 0, JAM2, 1, 1, NULL, "1", NULL };
  314.  
  315. struct Gadget pd7gadg =
  316. {   &xgadg,   526, 114, 11, 11, GADGIMAGE, RELVERIFY, BOOLGADGET,
  317.     (APTR)&image1, NULL, &itext30, 0, NULL, GADGPD7, NULL };
  318. struct Gadget pd6gadg =
  319. {   &pd7gadg, 512, 114, 11, 11, GADGIMAGE, RELVERIFY, BOOLGADGET,
  320.     (APTR)&image1, NULL, &itext29, 0, NULL, GADGPD6, NULL };
  321. struct Gadget pd5gadg =
  322. {   &pd6gadg, 498, 114, 11, 11, GADGIMAGE, RELVERIFY, BOOLGADGET,
  323.     (APTR)&image1, NULL, &itext28, 0, NULL, GADGPD5, NULL };
  324. struct Gadget pd4gadg =
  325. {   &pd5gadg, 484, 114, 11, 11, GADGIMAGE, RELVERIFY, BOOLGADGET,
  326.     (APTR)&image1, NULL, &itext27, 0, NULL, GADGPD4, NULL };
  327. struct Gadget pd3gadg =
  328. {   &pd4gadg, 470, 114, 11, 11, GADGIMAGE, RELVERIFY, BOOLGADGET,
  329.     (APTR)&image1, NULL, &itext26, 0, NULL, GADGPD3, NULL };
  330. struct Gadget pd2gadg =
  331. {   &pd3gadg, 456, 114, 11, 11, GADGIMAGE, RELVERIFY, BOOLGADGET,
  332.     (APTR)&image1, NULL, &itext25, 0, NULL, GADGPD2, NULL };
  333. struct Gadget pd1gadg =
  334. {   &pd2gadg, 442, 114, 11, 11, GADGIMAGE, RELVERIFY, BOOLGADGET,
  335.     (APTR)&image1, NULL, &itext24, 0, NULL, GADGPD1, NULL };
  336.  
  337. struct Gadget cwbgadg =
  338. {   &pd1gadg, 417, 226, 11, 11, GADGIMAGE, TOGGLESELECT, BOOLGADGET,
  339.     (APTR)&image1, NULL, NULL, 0, NULL, 0, NULL };
  340.  
  341. struct Gadget c4gadg =
  342. {   &cwbgadg, 553, 86, 11, 11, GADGIMAGE, RELVERIFY, BOOLGADGET,
  343.     (APTR)&image1, NULL, NULL, 0, NULL, GADGC4, NULL };
  344. struct Gadget c3gadg =
  345. {   &c4gadg, 553, 72, 11, 11, GADGIMAGE, RELVERIFY, BOOLGADGET,
  346.     (APTR)&image1, NULL, NULL, 0, NULL, GADGC3, NULL };
  347. struct Gadget bwgadg =
  348. {   &c3gadg, 553, 58, 11, 11, GADGIMAGE, RELVERIFY, BOOLGADGET,
  349.     (APTR)&image1, NULL, NULL, 0, NULL, GADGBW, NULL };
  350.  
  351. struct Gadget iffgadg =
  352. {   &bwgadg, 382, 86, 11, 11, GADGIMAGE, TOGGLESELECT, BOOLGADGET,
  353.     (APTR)&image1, NULL, NULL, 0, NULL, 0, NULL };
  354. struct Gadget scrgadg =
  355. {   &iffgadg, 382, 72, 11, 11, GADGIMAGE, TOGGLESELECT, BOOLGADGET,
  356.     (APTR)&image1, NULL, NULL, 0, NULL, 0, NULL };
  357. struct Gadget prigadg =
  358. {   &scrgadg, 382, 58, 11, 11, GADGIMAGE, TOGGLESELECT|RELVERIFY, BOOLGADGET,
  359.     (APTR)&image1, NULL, NULL, 0, NULL, GADGPRI, NULL };
  360.  
  361. SHORT bordervec3[] =
  362. {   0, 0, 101, 0, 101, 33, 0, 33, 0, 1 };
  363. struct Border border3 =
  364. {   -1, -1, 15, 0, JAM1, 5, bordervec3, NULL };
  365.  
  366. struct IntuiText itext1 =
  367. {   15, 0, JAM1, 26, 11, NULL, "CANCEL", NULL };
  368. struct Gadget cangadg =
  369. {   &prigadg, 174, 219, 100, 32, 0, RELVERIFY, BOOLGADGET,
  370.     (APTR)&border3, NULL, &itext1, 0, NULL, GADGCAN, NULL };
  371.  
  372. struct IntuiText itext2 =
  373. {   15, 0, JAM1, 43, 11, NULL, "OK", NULL };
  374. struct Gadget okgadg =
  375. {   &cangadg, 24, 219, 100, 32, 0, RELVERIFY, BOOLGADGET,
  376.     (APTR)&border3, NULL, &itext2, 0, NULL, GADGOK, NULL };
  377.  
  378. SHORT bordervec4[] =
  379. {   0, 0, 251, 0, 251, 14, 0, 14, 0, 1 };
  380. struct Border border4 =
  381. {   -2, -2, 15, 0, JAM1, 5, bordervec4, NULL };
  382.  
  383. UBYTE ifngadgbuff[80];
  384. struct StringInfo ifngadginfo =
  385. {   ifngadgbuff, (UBYTE *)undobuff, 0, 80, 0, 0, 0, 0, 0, 0, 0, 0, NULL };
  386. struct Gadget ifngadg =
  387. {   &okgadg, 25, 170, 250, 13, 0, 0, STRGADGET,
  388.     (APTR)&border4, NULL, NULL, 0, (APTR)&ifngadginfo, 0, NULL };
  389.  
  390. UBYTE if4gadgbuff[80];
  391. struct StringInfo if4gadginfo =
  392. {   if4gadgbuff, (UBYTE *)undobuff, 0, 80, 0, 0, 0, 0, 0, 0, 0, 0, NULL };
  393. struct Gadget if4gadg =
  394. {   &ifngadg, 25, 114, 250, 13, 0, 0, STRGADGET,
  395.     (APTR)&border4, NULL, NULL, 0, (APTR)&if4gadginfo, 0, NULL };
  396.  
  397. UBYTE if3gadgbuff[80];
  398. struct StringInfo if3gadginfo =
  399. {   if3gadgbuff, (UBYTE *)undobuff, 0, 80, 0, 0, 0, 0, 0, 0, 0, 0, NULL };
  400. struct Gadget if3gadg =
  401. {   &if4gadg, 25, 100, 250, 13, 0, 0, STRGADGET,
  402.     (APTR)&border4, NULL, NULL, 0, (APTR)&if3gadginfo, 0, NULL };
  403.  
  404. UBYTE if2gadgbuff[80];
  405. struct StringInfo if2gadginfo =
  406. {   if2gadgbuff, (UBYTE *)undobuff, 0, 80, 0, 0, 0, 0, 0, 0, 0, 0, NULL };
  407. struct Gadget if2gadg =
  408. {   &if3gadg, 25, 86, 250, 13, 0, 0, STRGADGET,
  409.     (APTR)&border4, NULL, NULL, 0, (APTR)&if2gadginfo, 0, NULL };
  410.  
  411. UBYTE if1gadgbuff[80];
  412. struct StringInfo if1gadginfo =
  413. {   if1gadgbuff, (UBYTE *)undobuff, 0, 80, 0, 0, 0, 0, 0, 0, 0, 0, NULL };
  414. struct Gadget if1gadg =
  415. {   &if2gadg, 25, 72, 250, 13, 0, 0, STRGADGET,
  416.     (APTR)&border4, NULL, NULL, 0, (APTR)&if1gadginfo, 0, NULL };
  417.  
  418. UBYTE if0gadgbuff[80];
  419. struct StringInfo if0gadginfo =
  420. {   if0gadgbuff, (UBYTE *)undobuff, 0, 80, 0, 0, 0, 0, 0, 0, 0, 0, NULL };
  421. struct Gadget if0gadg =
  422. {   &if1gadg, 25, 58, 250, 13, 0, 0, STRGADGET,
  423.     (APTR)&border4, NULL, NULL, 0, (APTR)&if0gadginfo, 0, NULL };
  424.  
  425. struct IntuiText itext23 =
  426. {   15, 0, JAM1, 300, 226, NULL, "Close WBench", NULL };
  427. struct IntuiText itext22 =
  428. {   15, 0, JAM1, 448, 212, NULL, "Paths", &itext23 };
  429. struct IntuiText itext21 =
  430. {   15, 0, JAM1, 448, 198, NULL, "VM", &itext22 };
  431. struct IntuiText itext20 =
  432. {   15, 0, JAM1, 448, 184, NULL, "Htone", &itext21 };
  433. struct IntuiText itext19 =
  434. {   15, 0, JAM1, 448, 170, NULL, "Fonts", &itext20 };
  435. struct IntuiText itext18 =
  436. {   15, 0, JAM1, 448, 142, NULL, "Memory", &itext19 };
  437. struct IntuiText itext17 =
  438. {   15, 0, JAM1, 300, 198, NULL, "Dpi.", &itext18 };
  439. struct IntuiText itext16 =
  440. {   15, 0, JAM1, 300, 184, NULL, "Off.", &itext17 };
  441. struct IntuiText itext15 =
  442. {   15, 0, JAM1, 300, 170, NULL, "X, Y", &itext16 };
  443. struct IntuiText itext14 =
  444. {   15, 0, JAM1, 300, 142, NULL, "Page size", &itext15 };
  445. struct IntuiText itext13 =
  446. {   15, 0, JAM1, 300, 114, NULL, "Printer density", &itext14 };
  447. struct IntuiText itext12 =
  448. {   15, 0, JAM1, 416, 86, NULL, "4 Colour (CMYK)", &itext13 };
  449. struct IntuiText itext11 =
  450. {   15, 0, JAM1, 416, 72, NULL, "3 Colour (RGB)", &itext12 };
  451. struct IntuiText itext10 =
  452. {   15, 0, JAM1, 416, 58, NULL, "Black and white", &itext11 };
  453. struct IntuiText itext9 =
  454. {   15, 0, JAM1, 300, 86, NULL, "IFF file", &itext10 };
  455. struct IntuiText itext8 =
  456. {   15, 0, JAM1, 300, 72, NULL, "Screen", &itext9 };
  457. struct IntuiText itext7 =
  458. {   15, 0, JAM1, 300, 58, NULL, "Printer", &itext8 };
  459. struct IntuiText itext6 =
  460. {   15, 0, JAM1, 416, 30, NULL, "Colours", &itext7 };
  461. struct IntuiText itext5 =
  462. {   15, 0, JAM1, 300, 30, NULL, "Output", &itext6 };
  463. struct IntuiText itext4 =
  464. {   15, 0, JAM1, 25, 156, NULL, "IFF file name (pattern)", &itext5 };
  465. struct IntuiText itext3 =
  466. {   15, 0, JAM1, 25, 30, NULL, "Startup file name(s)", &itext4 };
  467.  
  468. struct NewWindow newreqwindow =
  469. {   25, 60, 590, 260, 0, 15,
  470.     GADGETUP|GADGETDOWN, ACTIVATE|SMART_REFRESH|NOCAREREFRESH,
  471.     &if0gadg, NULL, POSTVER, NULL, NULL, 0, 0, 0, 0, CUSTOMSCREEN
  472. };
  473.  
  474. struct NewWindow newerrwindow =
  475. {   0, 100, 640, 100, 0, 15,
  476.     0, SIMPLE_REFRESH|NOCAREREFRESH|BACKDROP|BORDERLESS,
  477.     NULL, NULL, NULL, NULL, NULL, 0, 0, 0, 0, CUSTOMSCREEN
  478. };
  479.  
  480. /* Stuff for the windows */
  481.  
  482. struct PropInfo hscrollinfo =   /* Horizontal scroll proportional gadget */
  483. {   AUTOKNOB|FREEHORIZ, -1, -1, -1, -1 };
  484. struct Image hscrollimage;
  485. struct Gadget hscrollgadg =
  486. {   NULL, 0, -8, -14, 9,
  487.     GRELBOTTOM|GRELWIDTH, BOTTOMBORDER|RELVERIFY, PROPGADGET,
  488.     (APTR)&hscrollimage, NULL, NULL, 0, (APTR)&hscrollinfo, 0, NULL
  489. };
  490.  
  491. struct PropInfo vscrollinfo =   /* Vertical scroll proportional gadget */
  492. {   AUTOKNOB|FREEVERT, -1, -1, -1, -1 };
  493. struct Image vscrollimage;
  494. struct Gadget vscrollgadg =
  495. {   &hscrollgadg, -15, 0, 16, -8,
  496.     GRELRIGHT|GRELHEIGHT, RIGHTBORDER|RELVERIFY, PROPGADGET,
  497.     (APTR)&vscrollimage, NULL, NULL, 0, (APTR)&vscrollinfo, 0, NULL
  498. };
  499.  
  500. struct NewWindow newbitwindow = /* Bitmap window.  (Borders (4,2,18,10)) */
  501. {   0, 49, 640, 463, -1, -1,
  502.     0, SIMPLE_REFRESH,
  503.     &vscrollgadg, NULL, NULL, NULL, NULL,
  504.     0, 100, 0, 0, CUSTOMSCREEN
  505. };
  506.  
  507. struct NewWindow newintwindow = /* Interactive window */
  508. {   0, 12, 640, 37, -1, -1,
  509.     0, SIZEBRIGHT|WINDOWSIZING|SMART_REFRESH|ACTIVATE,
  510.     NULL, NULL, NULL, NULL, NULL,
  511.     0, 0, 0, 400, CUSTOMSCREEN
  512. };
  513.  
  514. struct IntuiText men2item3text = /* Menu strip */
  515. {   7, 0, COMPLEMENT, 0, 0, NULL, "Kill", NULL };
  516. struct MenuItem men2item3 =
  517. {   NULL, 0, 60, 200, 11, ITEMTEXT|ITEMENABLED|HIGHCOMP,
  518.     0, (APTR)&men2item3text, NULL, NULL, NULL, MENUNULL
  519. };
  520.  
  521. struct IntuiText men2item2text =
  522. {   7, 0, COMPLEMENT, 0, 0, NULL, "Interrupt", NULL };
  523. struct MenuItem men2item2 =
  524. {   &men2item3, 0, 40, 200, 11, ITEMTEXT|COMMSEQ|ITEMENABLED|HIGHCOMP,
  525.     0, (APTR)&men2item2text, NULL, 'I', NULL, MENUNULL
  526. };
  527.  
  528. struct IntuiText men2item1text =
  529. {   7, 0, COMPLEMENT, 0, 0, NULL, "Continue after pause", NULL };
  530. struct MenuItem men2item1 =
  531. {   &men2item2, 0, 20, 200, 11, ITEMTEXT|COMMSEQ|HIGHCOMP,
  532.     0, (APTR)&men2item1text, NULL, 'C', NULL, MENUNULL
  533. };
  534.  
  535. struct IntuiText men2item0text =
  536. {   7, 0, COMPLEMENT, 19, 0, NULL, "Pause every page", NULL };
  537. struct MenuItem men2item0 =
  538. {   &men2item1, 0, 0, 200,11,
  539.     CHECKIT|ITEMTEXT|COMMSEQ|ITEMENABLED|HIGHCOMP|CHECKED,
  540.     0, (APTR)&men2item0text, NULL, 'P', NULL, MENUNULL
  541. };
  542.  
  543. struct Menu menu2 =
  544. {   NULL, 160, 0, 80, 0, MENUENABLED, "Control", &men2item0 };
  545.  
  546. struct IntuiText men1item3text =
  547. {   7, 0, COMPLEMENT, 0, 0, NULL, "Interactive", NULL };
  548. struct MenuItem men1item3 =
  549. {   NULL, 0, 60, 128, 11, ITEMTEXT|COMMSEQ|ITEMENABLED|HIGHCOMP,
  550.     0, (APTR)&men1item3text, NULL, 'W', NULL, MENUNULL
  551. };
  552.  
  553. struct IntuiText men1item2text =
  554. {   7, 0, COMPLEMENT, 0, 0, NULL, "Run file", NULL };
  555. struct MenuItem men1item2 =
  556. {   &men1item3, 0, 40, 128, 11, ITEMTEXT|COMMSEQ|ITEMENABLED|HIGHCOMP,
  557.     0, (APTR)&men1item2text, NULL, 'R', NULL, MENUNULL
  558. };
  559.  
  560. struct IntuiText men1item1text =
  561. {   7, 0, COMPLEMENT, 0, 0, NULL, "Load file", NULL };
  562. struct MenuItem men1item1 =
  563. {   &men1item2, 0, 20, 128, 11, ITEMTEXT|COMMSEQ|ITEMENABLED|HIGHCOMP,
  564.     0, (APTR)&men1item1text, NULL, 'L', NULL, MENUNULL
  565. };
  566.  
  567. struct IntuiText men1item0text =
  568. {   7, 0, COMPLEMENT, 0, 0, NULL, "Load font", NULL };
  569. struct MenuItem men1item0 =
  570. {   &men1item1, 0, 0, 128, 11, ITEMTEXT|COMMSEQ|ITEMENABLED|HIGHCOMP,
  571.     0, (APTR)&men1item0text, NULL, 'F', NULL, MENUNULL
  572. };
  573.  
  574. struct Menu menu1 =
  575. {   &menu2, 80, 0, 80, 0, 0, "File", &men1item0 };
  576.  
  577. struct IntuiText men0item1text =
  578. {   7, 0, COMPLEMENT, 0, 0, NULL, "Quit", NULL };
  579. struct MenuItem men0item1 =
  580. {   NULL, 0, 20, 100, 11, ITEMTEXT|ITEMENABLED|HIGHCOMP,
  581.     0, (APTR)&men0item1text, NULL, NULL, NULL, MENUNULL
  582. };
  583.  
  584. struct IntuiText men0item0text =
  585. {   7, 0, COMPLEMENT, 0, 0, NULL, "Restart", NULL };
  586. struct MenuItem men0item0 =
  587. {   &men0item1, 0, 0, 100, 11, ITEMTEXT|ITEMENABLED|HIGHCOMP,
  588.     0, (APTR)&men0item0text, NULL, NULL, NULL, MENUNULL
  589. };
  590.  
  591. struct Menu menu0 =
  592. {   &menu1, 0, 0, 80, 0, 0, "Project", &men0item0 };
  593.  
  594. /* Arguments */
  595.  
  596. int arglen, argwb, argfilec, argsizec, argmemc;
  597. int argprint, argscreen, argiff, argint, argclosewb;
  598. char *argbuf, *argsizev[5], *argmemv[5];
  599. char *argfilev[] =
  600. {   (char *) &if0gadgbuff[0],
  601.     (char *) &if1gadgbuff[0],
  602.     (char *) &if2gadgbuff[0],
  603.     (char *) &if3gadgbuff[0],
  604.     (char *) &if4gadgbuff[0]
  605. };
  606. char *argifn =
  607.     (char *) &ifngadgbuff[0];
  608. char *argcon = "CON";
  609. char *argkey[] =
  610. {   "IFF",
  611.     "SCREEN",
  612.     "PRINTER",
  613.     "INTERACTIVE",
  614.     "SIZE",
  615.     "MEM",
  616.     "CLOSEWB",
  617.     "CONDEV",
  618.     NULL
  619. };
  620.  
  621. /* Lattice stuff for using Arp FPrintf/SPrintf */
  622.  
  623. LONG ArpFPrintf(BPTR, char*,);
  624. LONG ArpSPrintf(char *, char*,);
  625.  
  626. # pragma libcall ArpBase ArpFPrintf 00EA 98003
  627. # pragma libcall ArpBase ArpSPrintf 0282 98003
  628.  
  629. LONG __stdargs FPrintf(BPTR fh, char *format,)
  630. {   return  ArpFPrintf(fh, format, (&format)+1);
  631. }
  632.  
  633. LONG __stdargs SPrintf(char *string, char *format,)
  634. {   return  ArpSPrintf(string, format, (&format)+1);
  635. }
  636.  
  637. /* Startup code */
  638.  
  639. extern void main(int argc, char **argv);
  640.  
  641. void _main(char *line)
  642. {   struct DiskObject *diskobj;
  643.     char *diskname;
  644.     char *argv[32];
  645.     int argc;
  646.  
  647.     /* If this is a workbench startup construct the arguments string.  We
  648.      * find the name of our icon from the first argument.  Then we laod the
  649.      * icon and look for an ARGS string in the tooltypes.  We concatenate
  650.      * the program name and the arguments into an Alloc'ed buffer */
  651.  
  652.     if (WBenchMsg && WBenchMsg->sm_NumArgs)
  653.     {   diskname = WBenchMsg->sm_ArgList[0].wa_Name;
  654.         line = NULL;
  655.         IconBase = (struct IconBase *) OpenLibrary("icon.library", 0);
  656.         if (IconBase)
  657.         {   diskobj = GetDiskObject(diskname);
  658.             line = FindToolType(diskobj->do_ToolTypes, "ARGS");
  659.         }
  660.         if (line)
  661.         {   arglen = strlen(diskname) + strlen(line) + 2;
  662.             argbuf = AllocMem(arglen, MEMF_CLEAR);
  663.         }
  664.         if (argbuf)
  665.         {   strcpy(argbuf, diskname);
  666.             strcat(argbuf, " ");
  667.             strcat(argbuf, line);
  668.         }
  669.         line = argbuf;
  670.         if (diskobj)
  671.             FreeDiskObject(diskobj);
  672.         if (IconBase)
  673.             CloseLibrary((struct Library *) IconBase);
  674.     }
  675.  
  676.     /* Parse the arguments to break words and strip quotes.  N.B. the
  677.      * main program can determine that the arument was quoted by inspecting
  678.      * the preceeding character */
  679.  
  680.     argc = 0;
  681.     if (line == NULL) goto endline;
  682.     for (;;)
  683.     {   while (*line == ' ' || *line == '\t' || *line == '\n') line++;
  684.         if (*line == 0) break;
  685.         if (argc == 32)
  686.         {   argc = 0;
  687.             goto endline;
  688.         }
  689.         if (*line == '"')
  690.         {   argv[argc] = ++line;
  691.             while (*line != '"')
  692.             {   if (*line == 0)
  693.                 {   argc = 0;
  694.                     goto endline;
  695.                 }
  696.                 line++;
  697.             }
  698.         }
  699.         else
  700.         {   argv[argc] = line;
  701.             while (*line != ' ' && *line != '\t' && *line != '\n')
  702.             {   if (*line == 0)
  703.                 {   argc++;
  704.                     goto endline;
  705.                 }
  706.                 line++;
  707.             }
  708.         }
  709.         *line++ = 0;
  710.         argc++;
  711.     }
  712. endline:
  713.  
  714.     /* Set up the standard input/output files */
  715.  
  716.     if (WBenchMsg) /* Workbench startup, no files */
  717.     {   argwb = 1;
  718.     }
  719.     else           /* CLI startup, open stderr */
  720.     {   errfh = Open("*", MODE_OLDFILE);
  721.         if (errfh == NULL)
  722.         {   retcode = 20;
  723.             goto tidyexit;
  724.         }
  725.     }
  726.  
  727.     /* Call the main program  */
  728.  
  729.     main(argc, argv);
  730.  
  731.     /* Tidy up and exit */
  732.  
  733. tidyexit:
  734.     if (argbuf) FreeMem(argbuf, arglen);
  735.     if (errfh) Close(errfh);
  736.  
  737.     XCEXIT(retcode);
  738. }
  739.  
  740. /* Main program */
  741.  
  742. void main(int argc, char **argv)
  743. {   struct Message *msg;
  744.     struct Gadget *gadget;
  745.     char *s;
  746.     int *ip, i, ch, l;
  747.     int xsize, ysize, ssize, xoff, yoff, xden, yden, pden;
  748.  
  749.     process = (struct Process *) FindTask(NULL);
  750.     syswindow = (struct Window *) process->pr_WindowPtr;
  751.  
  752.     /* Open the libraries */
  753.  
  754.     GfxBase =
  755.         (struct GfxBase *)       OpenLibrary("graphics.library", 0);
  756.     IntuitionBase =
  757.         (struct IntuitionBase *) OpenLibrary("intuition.library", 0);
  758.     ArpBase =
  759.         (struct ArpBase *)       OpenLibrary("arp.library", 39);
  760.     if (GfxBase == NULL || IntuitionBase == NULL || ArpBase == NULL)
  761.     {   errmsg("can't open libraries (you need arp.library V39+)");
  762.         goto errorexit;
  763.     }
  764. # ifndef STATICLINK
  765.     PSbase = OpenLibrary("post.library", POSTVERNO);
  766.     if (PSbase == NULL)
  767.     {   errmsg("can't open post.library");
  768.         goto errorexit;
  769.     }
  770. # endif
  771.  
  772.     /* Parse the arguments and keywords.  See the usage string below */
  773.  
  774.     if (argc == 0) goto badargs;
  775.     argc--;
  776.     argv++;
  777.     if (argc == 0 || argwb) argint = 1;
  778.     if (argc == 1 && strcmp(*argv, "?") == 0) goto usage;
  779.  
  780.     while (argc--)
  781.     {   s = *argv++;
  782.         i = -1;
  783.         if (s[-1] != '"')
  784.             for (;;)
  785.             {   i++;
  786.                 if (argkey[i] == NULL)
  787.                 {   i = -1;
  788.                     break;
  789.                 }
  790.                 if (stricmp(s, argkey[i]) == 0) break;
  791.             }
  792.         switch (i)
  793.         {   case 0:    /* IFF */
  794.                 if (argc == 0 || strlen(*argv) >= 80) goto badargs;
  795.                 argc--;
  796.                 argiff = 1;
  797.                 strcpy(argifn, *argv++);
  798.                 break;
  799.  
  800.             case 1:    /* SCREEN */
  801.                 argscreen = 1;
  802.                 break;
  803.  
  804.             case 2:    /* PRINTER */
  805.                 argprint = 1;
  806.                 break;
  807.  
  808.             case 3:    /* INTERACTIVE */
  809.                 argint = 1;
  810.                 break;
  811.  
  812.             case 4:    /* SIZE */
  813.                 if (argc == 0) goto badargs;
  814.                 argc--;
  815.                 if (argsizec == 5) goto badargs;
  816.                 argsizev[argsizec++] = *argv++;
  817.                 break;
  818.  
  819.             case 5:    /* MEM */
  820.                 if (argc == 0) goto badargs;
  821.                 argc--;
  822.                 if (argmemc == 5) goto badargs;
  823.                 argmemv[argmemc++] = *argv++;
  824.                 break;
  825.  
  826.             case 6:    /* CLOSEWB */
  827.                 argclosewb = 1;
  828.                 break;
  829.  
  830.             case 7:    /* CONDEV */
  831.                 if (argc == 0) goto badargs;
  832.                 argc--;
  833.                 argcon = *argv++;
  834.                 break;
  835.  
  836.             default:
  837.                 if (argfilec == 5 || strlen(s) >= 80) goto badargs;
  838.                 strcpy(argfilev[argfilec++], s);
  839.         }
  840.     }
  841.     if (!argscreen && !argiff && !argint)  argprint = 1;
  842.     if (argscreen) argint = 1;
  843.  
  844.     /* Set up the screen size, adjusting for interlace */
  845.  
  846.     newscreen.Width = GfxBase->NormalDisplayColumns;
  847.     newscreen.Height = GfxBase->NormalDisplayRows * 2;
  848.     if (newscreen.Width < 400) newscreen.Width = 400;
  849.     if (newscreen.Height < 300) newscreen.Height = 300;
  850.  
  851.     /* Set up the default page size.  For printer output we get the defaults
  852.      * from preferences; otherwise we default to an A4 inch page at 75 dpi.
  853.      * with 3 colour planes, trimming the width to fit onto the screen.  (We
  854.      * can't quite fit A4 onto a standard 640 pixel wide screen - 8.24 inches
  855.      * instead of 8.26). */
  856.  
  857.     bitmap.BytesPerRow = 1;
  858.     bitmap.Rows = 1;
  859.     bitmap.Flags = 0;
  860.     bitmap.Depth = parm.page.depth = 3;
  861.     if (argprint)
  862.     {   setprinter();
  863.         if (prport == NULL)
  864.         {   errmsg("can't open printer device");
  865.             goto errorexit;
  866.         }
  867.     }
  868.     else
  869.     {   parm.page.xsize = 824 * 75 / 100;
  870.         parm.page.ysize = 1169 * 75 / 100;
  871.         parm.page.xoff = parm.page.yoff = 0;
  872.         parm.page.xden = parm.page.yden = 75;
  873.         if (parm.page.xsize > newscreen.Width - 22)
  874.             parm.page.xsize = newscreen.Width - 22;
  875.     }
  876.     parm.page.ydir = -1;
  877.  
  878.     /* Parse the "SIZE xyod..s..p.bc." options */
  879.  
  880.     xsize = ysize = ssize = xden = yden = pden = 0;
  881.     for (i = 0; i < argsizec; i++)
  882.     {   s = argsizev[i];
  883.         for (;;)
  884.         {   ch = *s++;
  885.             if (ch == 0) break;
  886.             ch = tolower(ch);
  887.             switch (ch)
  888.             {   case 'x':
  889.                     if      (tolower(*s) == 'o')
  890.                     {   s++;
  891.                         if (!strtoint(&s, &xoff)) goto badvalue;
  892.                         parm.page.xoff = xoff;
  893.                         continue;
  894.                     }
  895.                     else if (tolower(*s) == 'd')
  896.                     {   s++;
  897.                         ip = &xden;
  898.                     }
  899.                     else
  900.                         ip = &xsize;
  901.                     break;
  902.  
  903.                 case 'y':
  904.                     if      (tolower(*s) == 'o')
  905.                     {   s++;
  906.                         if (!strtoint(&s, &yoff)) goto badvalue;
  907.                         parm.page.yoff = yoff;
  908.                         continue;
  909.                     }
  910.                     else if (tolower(*s) == 'd')
  911.                     {   s++;
  912.                         ip = &yden;
  913.                     }
  914.                     else
  915.                         ip = &ysize;
  916.                     break;
  917.  
  918.                 case 's':
  919.                     ip = &ssize;
  920.                     break;
  921.  
  922.                 case 'p':
  923.                     ip = &pden;
  924.                     break;
  925.  
  926.                 case 'd':
  927.                 {   if (!strtoint(&s, &xden)) goto badvalue;
  928.                     yden = xden;
  929.                     continue;
  930.                 }
  931.  
  932.                 case 'b':
  933.                     parm.page.depth = 1;
  934.                     continue;
  935.  
  936.                 case 'c':
  937.                     ch = *s;
  938.                     if      (ch == '3')
  939.                     {   s++;
  940.                         parm.page.depth = 3;
  941.                     }
  942.                     else if (ch == '4')
  943.                     {   s++;
  944.                         parm.page.depth  = 4;
  945.                     }
  946.                     else
  947.                         parm.page.depth = 3;
  948.                     continue;
  949.  
  950.                 default:
  951.                     goto badvalue;
  952.             }
  953.             if (!strtoint(&s, ip)) goto badvalue;
  954.         }
  955.     }
  956.     if (xden != 0) parm.page.xden = xden;
  957.     if (yden != 0) parm.page.yden = yden;
  958.     if (xsize != 0) parm.page.xsize = xsize;
  959.     if (ysize != 0) parm.page.ysize = ysize;
  960.     if (pden != 0)
  961.     {   prden = pden;
  962.         if (argprint) setprintden();
  963.     }
  964.  
  965.     /* Set up the default memory sizes */
  966.  
  967.     parm.memvlen = defmemvlen;
  968.     parm.memflen = defmemflen;
  969.     parm.memllen = defmemllen;
  970.     parm.memhlen = defmemhlen;
  971.  
  972.     /* Parse the "MEM fhlv.." options */
  973.  
  974.     for (i = 0; i < argmemc; i++)
  975.     {   s = argmemv[i];
  976.         for (;;)
  977.         {   ch = *s++;
  978.             if (ch == 0) break;
  979.             ch = tolower(ch);
  980.             switch (ch)
  981.             {   case 'f':
  982.                     ip = &parm.memflen;
  983.                     break;
  984.  
  985.                 case 'h':
  986.                     ip = &parm.memhlen;
  987.                     break;
  988.  
  989.                 case 'l':
  990.                     ip = &parm.memllen;
  991.                     break;
  992.  
  993.                 case 'v':
  994.                     ip = &parm.memvlen;
  995.                     break;
  996.  
  997.                 default:
  998.                     goto badvalue;
  999.             }
  1000.             if (!strtoint(&s, ip)) goto badvalue;
  1001.         }
  1002.     }
  1003.  
  1004.     /* We only need the file requestors if we are interactive */
  1005.  
  1006.     if (argint)
  1007.     {   filereq1 = ArpAllocFreq();
  1008.         filereq2 = ArpAllocFreq();
  1009.         if (filereq1 == NULL || filereq2 == NULL)
  1010.         {   errmsg("can't get file requestors");
  1011.             goto errorexit;
  1012.         }
  1013.     }
  1014.  
  1015.     /* We seem to have to explicitly load the font from the disk before
  1016.      * Intuition can be depended upon to use it */
  1017.  
  1018.     if (argint)
  1019.     {  DiskfontBase = OpenLibrary("diskfont.library", 0);
  1020.        if (DiskfontBase)
  1021.        {   textfont = OpenDiskFont(&topaz11);
  1022.            CloseLibrary(DiskfontBase);
  1023.        }
  1024.        if (textfont == NULL)
  1025.        {   errmsg("can't find font topaz/11");
  1026.            goto errorexit;
  1027.        }
  1028.     }
  1029.  
  1030.     /* Set up the color map according to the number of bitplanes */
  1031.  
  1032. setcmap:
  1033.     restarterror = 0;
  1034.     if (!argint) newscreen.Depth = parm.page.depth;
  1035.     colormap.Count = 1 << newscreen.Depth;
  1036.     if      (newscreen.Depth == 1)
  1037.         colormap.ColorTable = (APTR) bcolors;
  1038.     else
  1039.         colormap.ColorTable = (APTR) ccolors;
  1040.  
  1041.     /* Open the screen and load the color map  */
  1042.  
  1043.     if (argint)
  1044.     {   screen = OpenScreen(&newscreen);
  1045.         if (screen == NULL)
  1046.         {   errmsg("can't open screen");
  1047.             goto errorexit;
  1048.         }
  1049.         newerrwindow.Screen = screen;
  1050.         newreqwindow.Screen = screen;
  1051.         newbitwindow.Screen = screen;
  1052.         newintwindow.Screen = screen;
  1053.         LoadRGB4(&screen->ViewPort,
  1054.                  (short *) colormap.ColorTable, colormap.Count);
  1055.         newerrwindow.Width = newscreen.Width;
  1056.         errwindow = OpenWindow(&newerrwindow);
  1057.         if (errwindow == NULL)
  1058.         {   errmsg("can't open error window");
  1059.             goto errorexit;
  1060.         }
  1061.         process->pr_WindowPtr = (APTR) errwindow;
  1062.     }
  1063.     restarterror = 1;
  1064.  
  1065. restart:
  1066.     if (fatalerror) goto tidyexit;
  1067.     retcode = 0;
  1068.     tidyup();
  1069.  
  1070.     if (argint)
  1071.     {   if (argclosewb) CloseWorkBench();
  1072.         if (requested == 0)
  1073.         {
  1074.             /* Put up the parameters requestor (window) */
  1075.  
  1076.             setreqgadgets();
  1077.             newreqwindow.IDCMPFlags = GADGETUP|GADGETDOWN;
  1078.             reqwindow = OpenWindow(&newreqwindow);
  1079.             if (reqwindow == NULL)
  1080.             {   errmsg("can't open parameters window");
  1081.                 goto restart;
  1082.             }
  1083.             PrintIText(reqwindow->RPort, &itext3, 0, 0);
  1084.  
  1085.             /* Loop handling gadget messages */
  1086.  
  1087.             for (;;)
  1088.             {   WaitPort(reqwindow->UserPort);
  1089.                 while (msg = GetMsg(reqwindow->UserPort))
  1090.                 {   gadget = (struct Gadget *)
  1091.                         ((struct IntuiMessage *) msg)->IAddress;
  1092.                     i = gadget->GadgetID;
  1093.                     switch (i)
  1094.                     {   case GADGOK:
  1095.                         case GADGCAN:
  1096.                             goto closereq;
  1097.  
  1098.                         case GADGPRI:
  1099.                             argprint = getgadgbool(gadget);
  1100.                             if (argprint)
  1101.                             {    setprinter();
  1102.                                  if (prport == NULL)
  1103.                                  {   argprint = 0;
  1104.                                      errmsg("can't open printer device");
  1105.                                      i = GADGRST;
  1106.                                      goto closereq;
  1107.                                  }
  1108.                             }
  1109.                             goto setgadg;
  1110.  
  1111.                         case GADGPD1: case GADGPD2: case GADGPD3:
  1112.                         case GADGPD4: case GADGPD5: case GADGPD6:
  1113.                         case GADGPD7:
  1114.                             if (argprint)
  1115.                             {   prden = i - (GADGPD1 - 1);
  1116.                                 setprintden();
  1117.                                 goto setgadg;
  1118.                             }
  1119.                             break;
  1120.  
  1121.                         case GADGBW:
  1122.                             parm.page.depth = 1;
  1123.                             goto setgadg;
  1124.  
  1125.                         case GADGC3:
  1126.                             parm.page.depth = 3;
  1127.                             goto setgadg;
  1128.  
  1129.                         case GADGC4:
  1130.                             parm.page.depth = 4;
  1131. setgadg:                    setreqgadgets();
  1132.                             break;
  1133.  
  1134.                         case GADGABS:
  1135.                             getgadgabs(gadget);
  1136.                     }
  1137.                     ReplyMsg(msg);
  1138.                 }
  1139.             }
  1140.  
  1141.             /* Close the requester window */
  1142.  
  1143. closereq:   ModifyIDCMP(reqwindow, CLOSEWINDOW);
  1144.             while (msg)
  1145.             {   ReplyMsg(msg);
  1146.                 msg = GetMsg(reqwindow->UserPort);
  1147.             }
  1148.             CloseWindow(reqwindow);
  1149.             reqwindow = NULL;
  1150.  
  1151.             if (i == GADGCAN) goto tidyexit;
  1152.             if (i == GADGRST) goto restart;
  1153.  
  1154.             getreqgadgets();
  1155.  
  1156.             /* If we have changed our mind about the number of bitplanes
  1157.              * close the screen and go back to reopen it with the new depth
  1158.              */
  1159.  
  1160.             if (parm.page.depth != newscreen.Depth)
  1161.             {   process->pr_WindowPtr = (APTR) syswindow;
  1162.                 CloseWindow(errwindow);
  1163.                 errwindow = NULL;
  1164.                 CloseScreen(screen);
  1165.                 screen = NULL;
  1166.                 newscreen.Depth = parm.page.depth;
  1167.                 requested = 1;
  1168.                 goto setcmap;
  1169.             }
  1170.         }
  1171.         requested = 0;
  1172.     }
  1173.  
  1174.     /* Set the size of the interactive windows */
  1175.  
  1176.     if (argint)
  1177.     {   newbitwindow.Width = newscreen.Width;
  1178.         newbitwindow.Height = newscreen.Height -
  1179.             newintwindow.TopEdge - newintwindow.Height;
  1180.         newbitwindow.TopEdge = newscreen.Height -
  1181.             newbitwindow.Height;
  1182.         newintwindow.Width = newscreen.Width;
  1183.         newintwindow.MaxHeight = newscreen.Height -
  1184.             newintwindow.TopEdge - newbitwindow.MinHeight;
  1185.  
  1186.     /* Locate the visible part of the bitmap within its window, adjusting
  1187.      * for the borders */
  1188.  
  1189.         winxbase = 4;
  1190.         winybase = 2;
  1191.         winxsize = newbitwindow.Width - 22;
  1192.         winysize = newbitwindow.Height - 12;
  1193.  
  1194.     /* Set the page size.  It must not be smaller than the interior of the
  1195.      * interactive window.  We may not need the horizontal scroll bar.  Each
  1196.      * bitmap row must start on a word boundary */
  1197.  
  1198.         if (parm.page.xsize <= winxsize)
  1199.         {   winysize += 8;
  1200.             vscrollgadg.NextGadget = 0;
  1201.             vscrollgadg.Height = 0;
  1202.         }
  1203.         else
  1204.         {   vscrollgadg.NextGadget = &hscrollgadg;
  1205.             vscrollgadg.Height = -8;
  1206.         }
  1207.         if (parm.page.xsize < winxsize) parm.page.xsize = winxsize;
  1208.         if (parm.page.ysize < winysize) parm.page.ysize = winysize;
  1209.     }
  1210.     parm.page.ybase = 0;
  1211.     parm.page.yheight = parm.page.ysize;
  1212.     if (!argint)
  1213.         if (ssize != 0 && ssize < parm.page.ysize) parm.page.ysize = ssize;
  1214.     if (parm.page.xsize == 0 || parm.page.ysize == 0)
  1215.     {   errmsg("page size not set in preferences");
  1216.         goto restart;
  1217.     }
  1218.     parm.page.xbytes = (parm.page.xsize + 15) >> 3 & 0xfffffffe;
  1219.     parm.page.len = parm.page.xbytes * parm.page.ysize;
  1220.  
  1221.     /* Allocate the page buffer.  It  must be in chip memory if we are
  1222.      * outputting to a screen */
  1223.  
  1224.     for (i = 0; i < parm.page.depth; i++)
  1225.     {   if ((parm.page.buf[i] = AllocMem(parm.page.len,
  1226.                  (argscreen ? MEMF_CHIP|MEMF_CLEAR :
  1227.                               MEMF_PUBLIC|MEMF_CLEAR))) == NULL)
  1228.         {   errmsg("can't get page buffer");
  1229.             goto restart;
  1230.         }
  1231.     }
  1232.  
  1233.     /* Initialise the bitmap */
  1234.  
  1235.     bitmap.BytesPerRow = parm.page.xbytes;
  1236.     bitmap.Rows = parm.page.ysize;
  1237.     bitmap.Flags = 0;
  1238.     bitmap.Depth = parm.page.depth;
  1239.     memcpy((char *) bitmap.Planes, (char *) parm.page.buf,
  1240.            sizeof bitmap.Planes);
  1241.  
  1242.     /* For interactive working, set up the windows */
  1243.  
  1244.     if (argint)
  1245.     {
  1246.         /* Finish initialising the gadgets and open the windows */
  1247.  
  1248.         hscrollinfo.HorizPot = 0;
  1249.         vscrollinfo.VertPot = 0xffff;
  1250.         hscrollinfo.HorizBody = (0xffff * winxsize) / parm.page.xsize;
  1251.         vscrollinfo.VertBody = (0xffff * winysize) / parm.page.ysize;
  1252.         winxpos = 0;
  1253.         winypos = parm.page.ysize - winysize;
  1254.         bitwindow = OpenWindow(&newbitwindow);
  1255.         intwindow = OpenWindow(&newintwindow);
  1256.         if (bitwindow == NULL || intwindow == NULL)
  1257.         {   errmsg("can't open windows");
  1258.             goto restart;
  1259.         }
  1260.  
  1261.         /* Set up new console streams.  Black on white characters */
  1262.  
  1263.         SPrintf((char *) undobuff, "%s:W%lx", argcon, intwindow);
  1264.         confh = Open((char *) undobuff, MODE_OLDFILE);
  1265.         if (confh == NULL)
  1266.         {   errmsg("can't open console device (you need ConMan V1.3+)");
  1267.             goto restart;
  1268.         }
  1269.         FPrintf(confh, "\x9b\x33\x37\x6d");
  1270.     }
  1271.  
  1272.         /* Create the menu handler task */
  1273.  
  1274.     if (argint)
  1275.     {   mainport = CreatePort(NULL, 0);
  1276.         if (mainport)
  1277.         {   menumsg.ExecMessage.mn_ReplyPort = mainport;
  1278.             menumsg.action = PSACTOPEN;
  1279.             menumsg.bitmap = &bitmap;
  1280.             menutask = CreateTask("post.menu", 6, (APTR) menuproc, 2000);
  1281.         }
  1282.         if (menutask)
  1283.             sendmenu(PSACTOPEN);
  1284.         if (menutask == NULL)
  1285.         {   errmsg("can't create menu handler");
  1286.             goto restart;
  1287.         }
  1288.     }
  1289.  
  1290.     /* Initialise for interpretation */
  1291.  
  1292.     insertbreak();
  1293.     SetExcept(~0, SIGBREAKF_CTRL_C);
  1294.     breakset = 1;
  1295.     insertftrap();
  1296.     ftrapset = 1;
  1297.  
  1298. # ifndef STATICLINK
  1299.     parm.flushfunc = (APTR) flushpage;
  1300.     parm.copyfunc = (APTR) copypage;
  1301. # endif
  1302.  
  1303.     if (argint)
  1304.     {   parm.infh = confh;
  1305.         parm.outfh = confh;
  1306.         parm.errfh = confh;
  1307.     }
  1308.     else
  1309.     {   parm.infh = Input();
  1310.         parm.outfh = Output();
  1311.         parm.errfh = errfh;
  1312.     }
  1313.  
  1314.     arec = PScreateact(&parm);
  1315.     if (arec == 0)
  1316.     {   errmsg("can't get memory");
  1317.         retcode = 20;
  1318.         goto restart;
  1319.     }
  1320.     if ((unsigned) arec <= errmax) retcode = 10;
  1321.  
  1322.     /* Interpret the argument files */
  1323.  
  1324.     for (i = 0; i < 5; i++)
  1325.     {   s = argfilev[i];
  1326.         if (s[0] != 0)
  1327.         {   if (retcode != 0) break;
  1328.             errcode =
  1329.                 PSintstring(arec, s, -1, PSFLAGFILE|PSFLAGCLEAR|PSFLAGERASE);
  1330.             if (errcode) retcode = 10;
  1331.         }
  1332.     }
  1333.  
  1334.     /* Execute menu commands */
  1335.  
  1336.     if (argint)
  1337.     {   for (;;)
  1338.         {   sendmenu(retcode == 0 ? PSACTCOMMAND : PSACTEXIT);
  1339.             s = menumsg.string;
  1340.             switch (menumsg.command)
  1341.             {   case PSCOMQUIT:
  1342.                     goto quit;
  1343.  
  1344.                 case PSCOMRESTART:
  1345.                     PSdeleteact(arec);
  1346.                     arec = 0;
  1347.                     goto restart;
  1348.  
  1349.                 case PSCOMFILEF:
  1350.                     i = PSFLAGFILE|PSFLAGCLEAR|PSFLAGERASE;
  1351.                     strcpy(filereq1->fr_Dir, "PSFonts:");
  1352.                     filereq = filereq1;
  1353.                     filereq->fr_Hail = hailfilef;
  1354.                     goto freq;
  1355.  
  1356.                 case PSCOMFILEL:
  1357.                     i = PSFLAGFILE|PSFLAGCLEAR|PSFLAGERASE;
  1358.                     filereq = filereq2;
  1359.                     filereq->fr_Hail = hailfilen;
  1360.                     goto freq;
  1361.  
  1362.                 case PSCOMFILER:
  1363.                     i = PSFLAGFILE|PSFLAGCLEAR|PSFLAGERASE|PSFLAGSAVE;
  1364.                     filereq = filereq2;
  1365.                     filereq->fr_Hail = hailfiles;
  1366. freq:               filereq->fr_Window = intwindow;
  1367.                     filereq->fr_FuncFlags |= (FRF_NewIDCMP|FRF_DoColor);
  1368.                     filereq->fr_LeftEdge = 170;
  1369.                     filereq->fr_TopEdge = 150;
  1370.                     if (FileRequest(filereq) == NULL) break;
  1371.                     l = strlen(filereq->fr_Dir);
  1372.                     TackOn(filereq->fr_Dir, filereq->fr_File);
  1373.                     PSintstring(arec, filereq->fr_Dir, -1, i);
  1374.                     filereq->fr_Dir[l] = 0;
  1375.                     break;
  1376.  
  1377.                 case PSCOMINTER:
  1378.                     PSintstring(arec, "%stdin", -1, 
  1379.                         PSFLAGFILE|PSFLAGCLEAR|PSFLAGERASE|PSFLAGSAVE|
  1380.                         PSFLAGINTER);
  1381.                     break;
  1382.             }
  1383.         }
  1384.     }
  1385.  
  1386. quit:
  1387.     PSdeleteact(arec);
  1388.     arec = 0;
  1389.     goto tidyexit;
  1390.  
  1391.     /* Argument errors and usage query */
  1392.  
  1393. badargs:
  1394.     errmsg("arguments bad, or value missing or too long");
  1395.     goto badusage;
  1396.  
  1397. badvalue:
  1398.     errmsg("argument bad value");
  1399.  
  1400. badusage:
  1401.     retcode = 20;
  1402.  
  1403. usage:
  1404.     if (!argwb) FPrintf(errfh, "post: usage:\n"
  1405.     "    post [files...] [IFF file] [SCREEN] [PRINTER] [INTERACTIVE]\n"
  1406.     "         [SIZE xyod..s..p.bc.] [MEM fhlv..] [CLOSEWB] [CONDEV con]\n");
  1407.     goto tidyexit;
  1408.  
  1409.     /* Tidy up and exit */
  1410.  
  1411. errorexit:
  1412.     retcode = 20;
  1413.  
  1414. tidyexit:
  1415.     tidyup();
  1416.  
  1417.     process->pr_WindowPtr = (APTR) syswindow;
  1418.     if (errwindow) CloseWindow(errwindow);
  1419.     if (screen) CloseScreen(screen);
  1420.  
  1421.     if (textfont) CloseFont(textfont);
  1422.  
  1423.     if (propen) CloseDevice((struct IORequest *) &prreq);
  1424.     if (prport) DeletePort(prport);
  1425.  
  1426. # ifndef STATICLINK
  1427.     if (PSbase)  CloseLibrary(PSbase);
  1428. # endif
  1429.     if (ArpBase) CloseLibrary((struct Library *) ArpBase);
  1430.     if (GfxBase) CloseLibrary((struct Library *) GfxBase);
  1431.     if (IntuitionBase)
  1432.     {   OpenWorkBench();
  1433.         CloseLibrary((struct Library *) IntuitionBase);
  1434.     }
  1435. }
  1436.  
  1437. /* Display an error requestor or message */
  1438.  
  1439. struct IntuiText bodytext2 =
  1440. {   AUTOFRONTPEN, AUTOBACKPEN, AUTODRAWMODE,
  1441.     AUTOLEFTEDGE + 32, AUTOTOPEDGE + 11,
  1442.     AUTOITEXTFONT, NULL, NULL };
  1443. struct IntuiText bodytext =
  1444. {   AUTOFRONTPEN, AUTOBACKPEN, AUTODRAWMODE,
  1445.     AUTOLEFTEDGE, AUTOTOPEDGE,
  1446.     AUTOITEXTFONT, POSTVER " Error", &bodytext2 };
  1447. struct IntuiText postext =
  1448. {   AUTOFRONTPEN, AUTOBACKPEN, AUTODRAWMODE,
  1449.     AUTOLEFTEDGE, AUTOTOPEDGE,
  1450.     AUTOITEXTFONT, "RETRY", NULL };
  1451. struct IntuiText negtext =
  1452. {   AUTOFRONTPEN, AUTOBACKPEN, AUTODRAWMODE,
  1453.     AUTOLEFTEDGE, AUTOTOPEDGE,
  1454.     AUTOITEXTFONT, "CANCEL", NULL };
  1455.  
  1456. void errmsg(char *string)
  1457. {   int width;
  1458.     if (IntuitionBase && (errwindow || argwb || ArpBase == NULL))
  1459.     {   bodytext2.IText = string;
  1460.         width = IntuiTextLength(&bodytext2) + 70;
  1461.         if (width < 200) width = 200;
  1462.         if (!AutoRequest(errwindow, &bodytext,
  1463.                 (restarterror ? &postext : NULL), &negtext,
  1464.                 0, 0, width, 70))
  1465.             fatalerror = 1;
  1466.         return;
  1467.     }
  1468.     if (ArpBase && !argwb)
  1469.     {   FPrintf(errfh, "post: %s\n", string);
  1470.         fatalerror = 1;
  1471.         return;
  1472.     }
  1473.     fatalerror = 1;
  1474. }
  1475.  
  1476. /* Tidy up */
  1477.  
  1478. void tidyup(void)
  1479. {   int i;
  1480.  
  1481.     if (breakset)
  1482.     {   SetExcept(0, SIGBREAKF_CTRL_C);
  1483.         deletebreak();
  1484.         breakset = 0;
  1485.     }
  1486.     if (ftrapset)
  1487.     {   deleteftrap();
  1488.         ftrapset = 0;
  1489.     }
  1490.  
  1491.     if (menuport)
  1492.         sendmenu(PSACTCLOSE);
  1493.     if (mainport)
  1494.     {   DeletePort(mainport);
  1495.         mainport = NULL;
  1496.     }
  1497.  
  1498.     if (confh)
  1499.     {   Close(confh);
  1500.         confh = NULL;
  1501.     }
  1502.  
  1503.     /* N.B. some versions of ConMan close the window themselves, so we
  1504.      * don't close the interactive window it it appears to have been
  1505.      * closed already */
  1506.  
  1507.     if (bitwindow)
  1508.     {   CloseWindow(bitwindow);
  1509.         bitwindow = NULL;
  1510.     }
  1511.     if (intwindow)
  1512.     {   if (screen->FirstWindow == intwindow ||
  1513.             errwindow->NextWindow == intwindow) CloseWindow(intwindow);
  1514.         intwindow = NULL;
  1515.     }
  1516.  
  1517.     for (i = 0; i < parm.page.depth; i++)
  1518.         if (parm.page.buf[i])
  1519.         {   FreeMem(parm.page.buf[i], parm.page.len);
  1520.             parm.page.buf[i] = NULL;
  1521.         }
  1522. }
  1523.  
  1524. /* Open the printer device and set up the page size */
  1525.  
  1526. void setprinter(void)
  1527. {   if (propen == 0)
  1528.     {   if (OpenDevice("printer.device", 0,
  1529.                        (struct IOReqest *) &prreq, 0) != 0)
  1530.             return;
  1531.         propen = 1;
  1532.     }
  1533.     if (prport == NULL)
  1534.     {   prport = CreatePort(NULL, 0);
  1535.         if (prport == NULL) return;
  1536.         prreq.io_Message.mn_ReplyPort = prport;
  1537.         prsig = 1 << prport->mp_SigBit;
  1538.         prdata = (struct PrinterData *) prreq.io_Device;
  1539.         prextdata = &prdata->pd_SegmentData->ps_PED;
  1540.         prprefs = &prdata->pd_Preferences;
  1541.         prden = prprefs->PrintDensity;
  1542.     }
  1543.     if ((prprefs->PrintShade & SHADE_COLOR) == 0) parm.page.depth = 1;
  1544.     setprintden();
  1545. }
  1546.  
  1547. /* Set the printer density */
  1548.  
  1549. void setprintden(void)
  1550. {   int pxsize, pysize, xy;
  1551.  
  1552.     /* New density, call the device driver to change its extended data.  No
  1553.      * error check */
  1554.  
  1555.     if (prden > 7) prden = 7;
  1556.     if (prden != prprefs->PrintDensity)
  1557.     {   prreq.io_Command = PRD_DUMPRPORT;
  1558.         prrast.BitMap = &bitmap;
  1559.         prreq.io_RastPort = &prrast;
  1560.         prreq.io_ColorMap = (struct ColorMap *) &colormap;
  1561.         prreq.io_Modes = 0;
  1562.         prreq.io_SrcX = 0;
  1563.         prreq.io_SrcY = 0;
  1564.         prreq.io_SrcWidth = 1;
  1565.         prreq.io_SrcHeight = 1;
  1566.         prreq.io_DestCols = 1;
  1567.         prreq.io_DestRows = 1;
  1568.         prreq.io_Special = (SPECIAL_DENSITY1 * prden) | SPECIAL_NOPRINT;
  1569.         prprefs->PrintDensity = prden;
  1570.         DoIO((struct IORequest *) &prreq);
  1571.     }
  1572.  
  1573.     /* Extract the page size and density from the printer device preferences
  1574.      * and extended data */
  1575.  
  1576.     parm.page.xden = prextdata->ped_XDotsInch;
  1577.     parm.page.yden = prextdata->ped_YDotsInch;
  1578.     if      (prprefs->PrintFlags & PIXEL_DIMENSIONS)
  1579.     {   pxsize = prprefs->PrintMaxWidth;
  1580.         pysize = prprefs->PrintMaxHeight;
  1581.     }
  1582.     else if (prprefs->PrintFlags &
  1583.                 (BOUNDED_DIMENSIONS|ABSOLUTE_DIMENSIONS))
  1584.     {   pxsize = prprefs->PrintMaxWidth * parm.page.xden / 10;
  1585.         pysize = prprefs->PrintMaxHeight * parm.page.yden / 10;
  1586.     }
  1587.     if (prprefs->PrintAspect & ASPECT_VERT)
  1588.     {   xy = pxsize;
  1589.         pxsize = pysize;
  1590.         pysize = xy;
  1591.     }
  1592.     if (pxsize != 0) parm.page.xsize = pxsize;
  1593.     if (pysize != 0) parm.page.ysize = pysize;
  1594.     parm.page.xoff = 0;
  1595.     parm.page.yoff = 0;
  1596. }
  1597.  
  1598. /* Set all requester gadgets */
  1599.  
  1600. void setreqgadgets(void)
  1601. {   setgadgbool(&bwgadg, (parm.page.depth == 1));
  1602.     setgadgbool(&c3gadg, (parm.page.depth == 3));
  1603.     setgadgbool(&c4gadg, (parm.page.depth == 4));
  1604.     setgadgbool(&pd1gadg, (prden == 1));
  1605.     setgadgbool(&pd2gadg, (prden == 2));
  1606.     setgadgbool(&pd3gadg, (prden == 3));
  1607.     setgadgbool(&pd4gadg, (prden == 4));
  1608.     setgadgbool(&pd5gadg, (prden == 5));
  1609.     setgadgbool(&pd6gadg, (prden == 6));
  1610.     setgadgbool(&pd7gadg, (prden == 7));
  1611.     setgadgint(&xgadg, parm.page.xsize);
  1612.     setgadgint(&ygadg, parm.page.ysize);
  1613.     setgadgint(&xogadg, parm.page.xoff);
  1614.     setgadgint(&yogadg, parm.page.yoff);
  1615.     setgadgint(&xdgadg, parm.page.xden);
  1616.     setgadgint(&ydgadg, parm.page.yden);
  1617.     if (reqwindow == NULL)
  1618.     {   setgadgbool(&prigadg, argprint);
  1619.         setgadgbool(&scrgadg, argscreen);
  1620.         setgadgbool(&iffgadg, argiff);
  1621.         setgadgbool(&cwbgadg, argclosewb);
  1622.         setgadgint(&mfgadg, parm.memflen);
  1623.         setgadgint(&mhgadg, parm.memhlen);
  1624.         setgadgint(&mvgadg, parm.memvlen);
  1625.         setgadgint(&mlgadg, parm.memllen);
  1626.     }
  1627.     enablegadg(&pd1gadg, argprint);
  1628.     enablegadg(&pd2gadg, argprint);
  1629.     enablegadg(&pd3gadg, argprint);
  1630.     enablegadg(&pd4gadg, argprint);
  1631.     enablegadg(&pd5gadg, argprint);
  1632.     enablegadg(&pd6gadg, argprint);
  1633.     enablegadg(&pd7gadg, argprint);
  1634. }
  1635.  
  1636. /* Get all requester gadgets */
  1637.  
  1638. void getreqgadgets(void)
  1639. {   argscreen = getgadgbool(&scrgadg);
  1640.     argiff = getgadgbool(&iffgadg);
  1641.     if (argifn[0] == 0) argiff = 0;
  1642.     parm.page.depth = getgadgbool(&bwgadg) ? 1 :
  1643.                       getgadgbool(&c3gadg) ? 3 : 4;
  1644.     argclosewb = getgadgbool(&cwbgadg);
  1645.     parm.page.xsize = getgadgabs(&xgadg);
  1646.     parm.page.ysize = getgadgabs(&ygadg);
  1647.     parm.page.xoff = getgadgint(&xogadg);
  1648.     parm.page.yoff = getgadgint(&yogadg);
  1649.     parm.page.xden = getgadgabs(&xdgadg);
  1650.     parm.page.yden = getgadgabs(&ydgadg);
  1651.     parm.memflen = getgadgabs(&mfgadg);
  1652.     parm.memhlen = getgadgabs(&mhgadg);
  1653.     parm.memvlen = getgadgabs(&mvgadg);
  1654.     parm.memllen = getgadgabs(&mlgadg);
  1655. }
  1656.  
  1657. /* Enable a gadget */
  1658.  
  1659. void enablegadg(struct Gadget *gadget, int enable)
  1660. {   if ((gadget->Flags & GADGDISABLED) != (enable ? 0 : GADGDISABLED))
  1661.     {   if (reqwindow)
  1662.             RemoveGadget(reqwindow, gadget);
  1663.         if (enable)
  1664.             gadget->Flags &= ~GADGDISABLED;
  1665.         else
  1666.             gadget->Flags |=  GADGDISABLED;
  1667.         if (reqwindow)
  1668.         {   AddGadget(reqwindow, gadget, 65535);
  1669.             RefreshGadgets(gadget, reqwindow, NULL);
  1670.         }
  1671.     }
  1672. }
  1673.  
  1674. /* Set the value of a boolean requester gadget */
  1675.  
  1676. void setgadgbool(struct Gadget *gadget, int value)
  1677. {   if (value == getgadgbool(gadget)) return;
  1678.     if (reqwindow)
  1679.         RemoveGadget(reqwindow, gadget);
  1680.     if (value)
  1681.         gadget->Flags |=  SELECTED;
  1682.     else
  1683.         gadget->Flags &= ~SELECTED;
  1684.     if (reqwindow)
  1685.     {   AddGadget(reqwindow, gadget, 65535);
  1686.         RefreshGadgets(gadget, reqwindow, NULL);
  1687.     }
  1688. }
  1689.  
  1690. /* Get the value of a boolean gadget */
  1691.  
  1692. int getgadgbool(struct Gadget *gadget)
  1693. {   return (gadget->Flags & SELECTED) != 0;
  1694. }
  1695.  
  1696. /* Set the value of an integer requester gadget */
  1697.  
  1698. void setgadgint(struct Gadget *gadget, int value)
  1699. {   struct StringInfo *info;
  1700.     if (reqwindow)
  1701.     {   if (value == getgadgint(gadget)) return;
  1702.         RemoveGadget(reqwindow, gadget);
  1703.     }
  1704.     info = (struct StringInfo *) gadget->SpecialInfo;
  1705.     info->LongInt = value;
  1706.     SPrintf((char *) info->Buffer, "%ld", value);
  1707.     if (reqwindow)
  1708.     {   AddGadget(reqwindow, gadget, 65535);
  1709.         RefreshGadgets(gadget, reqwindow, NULL);
  1710.     }
  1711. }
  1712.  
  1713. /* Get the value of an integer gadget */
  1714.  
  1715. int getgadgint(struct Gadget *gadget)
  1716. {   struct StringInfo *info = (struct StringInfo *) gadget->SpecialInfo;
  1717.     return (int) info->LongInt;
  1718. }
  1719.  
  1720. /* Get the absolute value of an integer gadget */
  1721.  
  1722. int getgadgabs(struct Gadget *gadget)
  1723. {   int value = getgadgint(gadget);
  1724.     if (value < 0)
  1725.     {   value = 0;
  1726.         setgadgint(gadget, value);
  1727.     }
  1728.     return value;
  1729. }
  1730.  
  1731. /* String to integer conversion; digits only, with error check */
  1732.  
  1733. int strtoint(char **sp, int *ip)
  1734. {   char *s = *sp;
  1735.     int i = 0;
  1736.     int ch;
  1737.     for (;;)
  1738.     {   ch = *s;
  1739.         if (ch < '0' || ch > '9') break;
  1740.         i = i * 10 + (ch - '0');
  1741.         s++;
  1742.     }
  1743.     if (s == *sp)
  1744.         return 0;
  1745.     else
  1746.     {   *sp = s;
  1747.         *ip = i;
  1748.         return 1;
  1749.     }
  1750. }
  1751.  
  1752. /* Send a mesage to the menu handler task */
  1753.  
  1754. void sendmenu(int action)
  1755. {   if (action != PSACTOPEN)
  1756.     {   menumsg.action = action;
  1757.         menumsg.result = retcode;
  1758.         menumsg.errnum = errcode;
  1759.         PutMsg(menuport, (struct Message *) &menumsg);
  1760.     }
  1761.     WaitPort(mainport);
  1762.     GetMsg(mainport);
  1763.     retcode = menumsg.result;
  1764. }
  1765.  
  1766. /* The menu handler task */
  1767.  
  1768. static int pause = 1;
  1769.  
  1770. void __saveds menuproc(void)
  1771. {   struct Message *msg;
  1772.     char *title, *oldtitle, *savetitle;
  1773.     int height, i, j;
  1774.  
  1775.     title = titlestart;
  1776.     height = newintwindow.Height;
  1777.     msg = (struct Message *) &menumsg;
  1778.  
  1779.     /* Loop handling messages.  We access the initial message directly,
  1780.      * rather than getting it from the menu port, as we havn't created the
  1781.      * port yet */
  1782.  
  1783.     for (;;)
  1784.     {
  1785.         /* Message from main program */
  1786.  
  1787.         if ((struct PSmessage *) msg == &menumsg)
  1788.         {   switch (menumsg.action)
  1789.             {
  1790.                 /* Open the windows and initialise */
  1791.  
  1792.                 case PSACTOPEN:
  1793.                     menuport = CreatePort(NULL, 0);
  1794.                     if (menuport == NULL)
  1795.                     {   menumsg.result = 20;
  1796.                         goto end;
  1797.                     }
  1798.                     bitwindow->UserPort = menuport;
  1799.                     intwindow->UserPort = menuport;
  1800.                     ModifyIDCMP(bitwindow,
  1801.                             REFRESHWINDOW|ACTIVEWINDOW|GADGETUP);
  1802.                     ModifyIDCMP(intwindow,
  1803.                             REFRESHWINDOW|MENUPICK);
  1804.                     menu2.Flags |= MENUENABLED;
  1805.                     SetMenuStrip(intwindow, &menu0);
  1806.                     SetWindowTitles(intwindow, NULL, (UBYTE *) title);
  1807.                     ReplyMsg(msg);
  1808.                     break;
  1809.  
  1810.                 /* Close the windows and tidy up */
  1811.  
  1812.                 case PSACTCLOSE:
  1813.                     ClearMenuStrip(intwindow);
  1814.                     ModifyIDCMP(bitwindow, CLOSEWINDOW);
  1815.                     ModifyIDCMP(intwindow, CLOSEWINDOW);
  1816.                     bitwindow->UserPort = NULL;
  1817.                     intwindow->UserPort = NULL;
  1818.                     for (;;)
  1819.                     {   msg = GetMsg(menuport);
  1820.                         if (msg == NULL) break;
  1821.                         ReplyMsg(msg);
  1822.                     }
  1823.                     DeletePort(menuport);
  1824.                     menuport = NULL;
  1825.                     goto end;
  1826.  
  1827.                 /* Flush the bitmap to its window */
  1828.  
  1829.                 case PSACTFLUSH:
  1830.                     i = menumsg.y1;
  1831.                     j = menumsg.y2;
  1832.                     if (i < winypos) i = winypos;
  1833.                     if (j < winypos) j = winypos;
  1834.                     if (i > winypos + winysize) i = winypos + winysize;
  1835.                     if (j > winypos + winysize) j = winypos + winysize;
  1836.                     if (j > i && argscreen)
  1837.                     {   BltBitMapRastPort(&bitmap, winxpos, i,
  1838.                                           bitwindow->RPort,
  1839.                                           winxbase, winybase + i - winypos,
  1840.                                           winxsize, j - i, 0xC0);
  1841.                     }
  1842.                     ReplyMsg(msg);
  1843.                     break;
  1844.  
  1845.                 /* Pause at the end of a page */
  1846.  
  1847.                 case PSACTPAUSE:
  1848.                     if (pause)
  1849.                     {   savetitle = title;
  1850.                         title = titlepaused;
  1851.                         SetWindowTitles(intwindow, NULL, (UBYTE *) title);
  1852.                         OnMenu(intwindow, 2 | SHIFTITEM(1));
  1853.                     }
  1854.                     else
  1855.                         ReplyMsg(msg);
  1856.                     break;
  1857.  
  1858.                 /* Get the next command */
  1859.  
  1860.                 case PSACTCOMMAND:
  1861.                     title = titlewait;
  1862.                     SetWindowTitles(intwindow, NULL, (UBYTE *) title);
  1863.                     OnMenu(intwindow, 0 | SHIFTITEM(NOITEM));
  1864.                     OnMenu(intwindow, 1 | SHIFTITEM(NOITEM));
  1865.                     break;
  1866.  
  1867.                 /* Get a quit or restart command */
  1868.  
  1869.                 case PSACTEXIT:
  1870.                     title = titlewait;
  1871.                     SetWindowTitles(intwindow, NULL, (UBYTE *) title);
  1872.                     OnMenu(intwindow, 0 | SHIFTITEM(NOITEM));
  1873.                     OffMenu(intwindow, 2 | SHIFTITEM(NOITEM));
  1874.                     break;
  1875.             }
  1876.         }
  1877.  
  1878.         /* Message from Intuition */
  1879.  
  1880.         else
  1881.         {   switch (((struct IntuiMessage *) msg)->Class)
  1882.             {
  1883.                 /* Make the interactive window the active one, but only if
  1884.                  * the scroll gadget knobs in the bitmapped window are not
  1885.                  * currently hit (so as not to prevent dragging them). (If
  1886.                  * someone is dragging them we will get a GADGETUP message
  1887.                  * when he lets go.) */
  1888.  
  1889.                 case ACTIVEWINDOW:
  1890.                     if (!(hscrollgadg.Flags & SELECTED) &&
  1891.                         !(vscrollgadg.Flags & SELECTED))
  1892.                         ActivateWindow(intwindow);
  1893.                     break;
  1894.  
  1895.                 /* Refresh a window */
  1896.  
  1897.                 case REFRESHWINDOW:
  1898.  
  1899.                     /* Refresh the bitmapped window */
  1900.  
  1901.                     if (((struct IntuiMessage *)msg)->IDCMPWindow ==
  1902.                                                                  bitwindow)
  1903.                     {   BeginRefresh(bitwindow);
  1904.                         if (argscreen)
  1905.                             BltBitMapRastPort(&bitmap, winxpos, winypos,
  1906.                                               bitwindow->RPort,
  1907.                                               winxbase, winybase,
  1908.                                               winxsize, winysize, 0xC0);
  1909.                         EndRefresh(bitwindow, TRUE);
  1910.                     }
  1911.  
  1912.                     /* We don't actually refresh the interactive window, but
  1913.                      * instead we use this event to tell us when it has
  1914.                      * changed size, so we can adjust the size and position
  1915.                      * of the bitmapped window to match.  (Despite what the
  1916.                      * manual says we seem to get the refresh message even
  1917.                      * when the window gets smaller */
  1918.  
  1919.                     else
  1920.                     {   i = intwindow->Height;
  1921.                         j = i - height;
  1922.                         height = i;
  1923.                         if      (j < 0)
  1924.                         {   MoveWindow(bitwindow, 0, j);
  1925.                             SizeWindow(bitwindow, 0, -j);
  1926.                         }
  1927.                         else if (j > 0)
  1928.                         {   SizeWindow(bitwindow, 0, -j);
  1929.                             MoveWindow(bitwindow, 0, j);
  1930.                         }
  1931.                         winypos += j;
  1932.                         if (winypos < 0) winypos = 0;
  1933.                         winysize -= j;
  1934.                         ModifyProp(&vscrollgadg, bitwindow, NULL,
  1935.                             AUTOKNOB|FREEVERT,
  1936.                             -1, (0xffff * winypos) /
  1937.                                     (parm.page.ysize - winysize),
  1938.                             -1, (0xffff * winysize) / parm.page.ysize);
  1939.                     }
  1940.  
  1941.                     break;
  1942.  
  1943.                 /* Scroll the bitmapped window.  Make sure we can scroll to
  1944.                  * the edges of the page, even after rounding erors */
  1945.  
  1946.                 case GADGETUP:
  1947.                     ActivateWindow(intwindow);
  1948.                     if      (((struct IntuiMessage *) msg)->IAddress ==
  1949.                                     (APTR) &hscrollgadg)
  1950.                     {   i = (parm.page.xsize - winxsize);
  1951.                         j = (i * hscrollinfo.HorizPot) / 65535;
  1952.                         if (j < i / 30) j = 0;
  1953.                         if (i - j < i / 30) j = i;
  1954.                         winxpos = j;
  1955.                     }
  1956.                     else if (((struct IntuiMessage *) msg)->IAddress ==
  1957.                                     (APTR) &vscrollgadg)
  1958.                     {   i = (parm.page.ysize - winysize);
  1959.                         j = (i * vscrollinfo.VertPot) / 65535;
  1960.                         if (j < i / 30) j = 0;
  1961.                         if (i - j < i / 30) j = i;
  1962.                         winypos = j;
  1963.                     }
  1964.                     if (argscreen)
  1965.                         BltBitMapRastPort(&bitmap, winxpos, winypos,
  1966.                                           bitwindow->RPort,
  1967.                                           winxbase, winybase,
  1968.                                           winxsize, winysize, 0xC0);
  1969.                     break;
  1970.  
  1971.                 /* Menu selection.  We don't implement extended selection
  1972.                  * to avoid real time problems, and it wouldn't be useful
  1973.                  * for our range of choices anyway */
  1974.  
  1975.                 case MENUPICK:
  1976.                     i = ((struct IntuiMessage *) msg)->Code;
  1977.                     j = ITEMNUM(i);
  1978.                     i = MENUNUM(i);
  1979.                     oldtitle = title;
  1980.                     if      (i == 0)     /* Project */
  1981.                     {   if      (j == 0) /*   Restart */
  1982.                             menumsg.command = PSCOMRESTART;
  1983.                         else if (j == 1) /*   Quit */
  1984.                             menumsg.command = PSCOMQUIT;
  1985.                     }
  1986.                     else if (i == 1)     /* File */
  1987.                     {   title = titlerunning;
  1988.                         if      (j == 0) /*   Load font */
  1989.                             menumsg.command = PSCOMFILEF;
  1990.                         else if (j == 1) /*   Load file */
  1991.                             menumsg.command = PSCOMFILEL;
  1992.                         else if (j == 2) /*   Run file */
  1993.                             menumsg.command = PSCOMFILER;
  1994.                         else if (j == 3) /*   Interactive */
  1995.                         {   menumsg.command = PSCOMINTER;
  1996.                             title = titleinter;
  1997.                         }
  1998.                     }
  1999.                     else if (i == 2)     /* Control */
  2000.                     {   if      (j == 0) /*   Pause every page */
  2001.                         {   pause = !pause;
  2002.                             if (pause)
  2003.                                 men2item0.Flags |=  CHECKED;
  2004.                             else
  2005.                                 men2item0.Flags &= ~CHECKED;
  2006.                             break;
  2007.                         }
  2008.                         else if (j == 1) /*   Continue after pause */
  2009.                         {   menumsg.command = 0;
  2010.                             title = savetitle;
  2011.                             OffMenu(intwindow, 2 | SHIFTITEM(1));
  2012.                         }
  2013.                         else if (j == 2) /*   Interrupt */
  2014.                         {   PSsignalint(arec, 1);
  2015.                             break;
  2016.                         }
  2017.                         else if (j == 3) /*   Kill */
  2018.                         {   PSsignalint(arec, 2);
  2019.                             break;
  2020.                         }
  2021.                     }
  2022.                     else                 /* NULL */
  2023.                         break;
  2024.                     OffMenu(intwindow, 0 | SHIFTITEM(NOITEM));
  2025.                     OffMenu(intwindow, 1 | SHIFTITEM(NOITEM));
  2026.                     if (title != oldtitle)
  2027.                         SetWindowTitles(intwindow, NULL, (UBYTE *) title);
  2028.                     menumsg.length = -1;
  2029.                     menumsg.string = NULL;
  2030.                     ReplyMsg((struct Message *) &menumsg);
  2031.                     break;
  2032.             }
  2033.             ReplyMsg(msg);
  2034.         }
  2035.  
  2036.         /* Get next message */
  2037.  
  2038.         for (;;)
  2039.         {   msg = GetMsg(menuport);
  2040.             if (msg) break;
  2041.             WaitPort(menuport);
  2042.         }
  2043.     }
  2044.  
  2045.     /* Open failure or close.  Reply remove our task */
  2046.  
  2047. end:
  2048.     Forbid();
  2049.     ReplyMsg((struct Message *) &menumsg);
  2050.     menutask = NULL;
  2051.     RemTask(menutask);
  2052. }
  2053.  
  2054. /* Signal an interrupt */
  2055.  
  2056. void __saveds sigint()
  2057. {   PSsignalint(arec, 1);
  2058. }
  2059.  
  2060. /* Signal a floating point error */
  2061.  
  2062. void __saveds sigfpe()
  2063. {   PSsignalfpe(arec);
  2064. }
  2065.  
  2066. /* Call an external function (dummy) */
  2067.  
  2068. # ifdef STATICLINK
  2069. int callextfunc(APTR func, APTR aptr, int parms)
  2070. {   return 0;
  2071. }
  2072. # endif
  2073.  
  2074. /* Flush the page to the screen */
  2075.  
  2076. # ifdef STATICLINK
  2077. void flushpage(int y1, int y2)
  2078. # else
  2079. void __saveds __asm flushpage(register __d0 int y1, register __d1 int y2)
  2080. # endif
  2081. {   if (argscreen)
  2082.     {   menumsg.y1 = y1;
  2083.         menumsg.y2 = y2;
  2084.         sendmenu(PSACTFLUSH);
  2085.     }
  2086. }
  2087.  
  2088. /* Copy the page to the output */
  2089.  
  2090. # ifdef STATICLINK
  2091. void copypage(int num)
  2092. # else
  2093. void __saveds __asm copypage(register __d0 int num)
  2094. # endif
  2095. {   ioerror = 0;
  2096.     if (argprint)
  2097.         while (num--) printpage();
  2098.     if (argiff)
  2099.         iffpage();
  2100.     if (argscreen)
  2101.         sendmenu(PSACTPAUSE);
  2102.     if (ioerror) PSerror(arec, ioerror);
  2103. }
  2104.  
  2105. /* Print the page */
  2106.  
  2107. void printpage()
  2108. {   ULONG sig;
  2109.     UWORD prflags;
  2110.  
  2111.     /* Disable break exceptions so we can wait on the signal instead */
  2112.  
  2113.     SetExcept(0, SIGBREAKF_CTRL_C);
  2114.     breakset = 0;
  2115.  
  2116.     /* First check the printer is ready */
  2117.  
  2118.     prreq.io_Command = PRD_QUERY;
  2119.     ((struct IOStdReq *) &prreq)->io_Data = (APTR) prstatus;
  2120.     if (DoIO((struct IORequest *) &prreq))
  2121.     {   ioerror = errioerror;
  2122.         return;
  2123.     }
  2124.     if (((struct IOStdReq *) &prreq)->io_Actual == 1 && prstatus[0] & 3 != 0)
  2125.         FPrintf(argint ? confh : errfh,
  2126.                 "post: printer not ready (CTRL/C to abort)\n");
  2127.  
  2128.     /* Now dump the page */
  2129.  
  2130.     prrast.BitMap = &bitmap;
  2131.     prreq.io_Command = PRD_DUMPRPORT;
  2132.     prreq.io_RastPort = &prrast;
  2133.     prreq.io_ColorMap = (struct ColorMap *) &colormap;
  2134.     prreq.io_Modes = 0;
  2135.     prreq.io_SrcX = 0;
  2136.     prreq.io_SrcY = 0;
  2137.     prreq.io_SrcWidth = parm.page.xsize;
  2138.     prreq.io_SrcHeight = parm.page.ysize;
  2139.     prreq.io_Special = (SPECIAL_DENSITY1 * prden) | SPECIAL_TRUSTME;
  2140.     if (parm.page.ybase + parm.page.ysize >= parm.page.yheight)
  2141.         prreq.io_SrcHeight = parm.page.yheight - parm.page.ybase;
  2142.     else
  2143.         prreq.io_Special |= SPECIAL_NOFORMFEED;
  2144.     if (prextdata->ped_MaxXDots != 0)
  2145.         if (prreq.io_SrcWidth > prextdata->ped_MaxXDots)
  2146.             prreq.io_SrcWidth = prextdata->ped_MaxXDots;
  2147.     if (prextdata->ped_MaxYDots != 0)
  2148.         if (prreq.io_SrcHeight > prextdata->ped_MaxYDots)
  2149.             prreq.io_SrcHeight = prextdata->ped_MaxYDots;
  2150.     prreq.io_DestCols = prreq.io_SrcWidth;
  2151.     prreq.io_DestRows = prreq.io_SrcHeight;
  2152.     prflags = prprefs->PrintFlags;
  2153.     prprefs->PrintFlags = prflags & ~DIMENSIONS_MASK | IGNORE_DIMENSIONS;
  2154.  
  2155.     /* We use asynchronous IO so we can abort it with a CTRL/C */
  2156.  
  2157.     SendIO((struct IORequest *) &prreq);
  2158.  
  2159.     for (;;)
  2160.     {   sig = Wait(prsig | SIGBREAKF_CTRL_C);
  2161.         if (sig & SIGBREAKF_CTRL_C)
  2162.         {   AbortIO((struct IORequest *) &prreq);
  2163.             WaitIO((struct IORequest *) &prreq);
  2164.             ioerror = errioerror;
  2165.             break;
  2166.         }
  2167.         if (GetMsg(prport))
  2168.             break;
  2169.     }
  2170.     if (prreq.io_Error != 0) ioerror = errioerror;
  2171.  
  2172.     /* Restore break exceptions */
  2173.  
  2174.     SetExcept(~0, SIGBREAKF_CTRL_C);
  2175.     breakset = 1;
  2176.  
  2177.     prprefs->PrintFlags = prflags;
  2178. }
  2179.  
  2180. /* IFF ILBM routines */
  2181.  
  2182. static int iffseq = 0;
  2183.  
  2184. static int ifferr;
  2185.  
  2186. static FILE *ifffptr;
  2187.  
  2188. /* Put a byte */
  2189.  
  2190. static void iffputb(int b)
  2191. {   if (ifferr) return;
  2192.     if (putc((int) b, ifffptr) == EOF)
  2193.     {   ifferr = 1;
  2194.         return;
  2195.     }
  2196. }
  2197.  
  2198. /* Put a word */
  2199.  
  2200. static void iffputw(int w)
  2201. {   iffputb(w>>8);
  2202.     iffputb(w);
  2203. }
  2204.  
  2205. /* Put a long */
  2206.  
  2207. static void iffputl(int l)
  2208. {   iffputb(l>>24);
  2209.     iffputb(l>>16);
  2210.     iffputb(l>>8);
  2211.     iffputb(l);
  2212. }
  2213.  
  2214. /* Put a string */
  2215.  
  2216. static void iffputs(char *str)
  2217. {   while (*str) iffputb(*str++);
  2218. }
  2219.  
  2220. /* Pack a bitmap row */
  2221.  
  2222. static void iffpack(char *buf, int len)
  2223. {   int b, c, l;
  2224.     if (ifferr) return;
  2225.     l = 0;
  2226.     while (len--)
  2227.     {   b = *buf++;                  /* Pick up a byte */
  2228.         c = 1;
  2229.         while (len && *buf == b && c < 128)
  2230.         {   c++;
  2231.             buf++;
  2232.             len--;                   /* See if it begins a run */
  2233.         }
  2234.         if (c == 2 &&                /* If a two byte run */
  2235.             l > 0 &&                 /*    and preceeded by literals */
  2236.             l <= 125 &&              /*    and not more than 125 of them */
  2237.             (len <= 2 ||             /*    and no more than 2 bytes left */
  2238.              *buf != *(buf + 1)))    /*        or not followed by a run */
  2239.         {   c = 1;                   /* Then make it a literal */
  2240.             buf--;
  2241.             len++;
  2242.         }
  2243.         if (c == 1)                  /* If not a run */
  2244.         {   l++;                     /* Then it must be a literal */
  2245.             c = 0;
  2246.         }
  2247.         if (l > 0 &&                 /* If we have some literals */
  2248.             (c > 1 ||                /*    and beginning a run */
  2249.              l == 127 ||             /*    or reached 127 */
  2250.              len == 0))              /*    or no more bytes left */
  2251.         {   if (putc(l - 1, ifffptr) == EOF)
  2252.             {   ifferr = 1;
  2253.                 return;
  2254.             }
  2255.             while (l)              /* Then write out  the literals */
  2256.             {   if (putc(*(buf - c - l), ifffptr) == EOF)
  2257.                 {   ifferr = 1;
  2258.                     return;
  2259.                 }
  2260.                 l--;
  2261.             }
  2262.         }
  2263.         if (c > 1)                   /* If we have a run, write it */
  2264.         {   if (putc(1 - c, ifffptr) == EOF)
  2265.             {   ifferr = 1;
  2266.                 return;
  2267.             }
  2268.             if (putc(b, ifffptr) == EOF)
  2269.             {   ifferr = 1;
  2270.                 return;
  2271.             }
  2272.         }
  2273.     }
  2274. }
  2275.  
  2276. /* Determine the current address */
  2277.  
  2278. static long ifftell(void)
  2279. {   long addr;
  2280.     if (ifferr) return 0;
  2281.     if ((addr = ftell(ifffptr)) == -1)
  2282.     {   ifferr = 1;
  2283.         return 0;
  2284.     }
  2285.     return addr;
  2286. }
  2287.  
  2288. /* Fix up the length of a chunk */
  2289.  
  2290. static void ifffixup(long addr)
  2291. {   long size;
  2292.     if (ifferr) return;
  2293.     if ((size = ftell(ifffptr)) == -1)
  2294.     {   ifferr = 1;
  2295.         return;
  2296.     }
  2297.     if (size & 1) iffputb(0);
  2298.     size = size - addr - 8;
  2299.     if (fseek(ifffptr, addr + 4, 0) != 0)
  2300.     {   ifferr = 1;
  2301.         return;
  2302.     }
  2303.     iffputl(size);
  2304.     if (fseek(ifffptr, 0, 2) != 0)
  2305.     {   ifferr = 1;
  2306.         return;
  2307.     }
  2308. }
  2309.  
  2310. /* Write the page to the iff output file */
  2311.  
  2312. void iffpage(void)
  2313. {   long addr1, addr2;
  2314.     int xa, ya;
  2315.     int i, j, k, ch;
  2316.     UWORD w;
  2317.     int fslen;
  2318.     char fname[110], fsnum[10];
  2319.  
  2320.     /* Compute the aspect ratio.  Make sure the values fit into a byte */
  2321.  
  2322.     xa = parm.page.yden;
  2323.     ya = parm.page.xden;
  2324.     i = igcd(xa, ya);
  2325.     xa /= i;
  2326.     ya /= i;
  2327.     while (xa > 255 && ya > 255)
  2328.     {   xa /= 2;
  2329.         ya /= 2;
  2330.     }
  2331.  
  2332.     /* Construct the file name.  Copy it, replacing "*" by the sequence
  2333.      * number.  The scan it backwards replacing "?" by digits */
  2334.  
  2335.     iffseq++;
  2336.     fslen = 0;
  2337.     i = iffseq;
  2338.     while (i)
  2339.     {   fsnum[fslen++] = i % 10 + '0';
  2340.         i /= 10;
  2341.     }
  2342.     i = j = 0;
  2343.     for (;;)
  2344.     {   if (j > 100)
  2345.         {   ioerror = errioerror;
  2346.             return;
  2347.         }
  2348.         ch = argifn[i++];
  2349.         if (ch == '*')
  2350.         {   k = fslen;
  2351.             while (k--) fname[j++] = fsnum[k];
  2352.         }
  2353.         else
  2354.            fname[j++] = ch;
  2355.         if (ch == 0) break;
  2356.     }
  2357.     k = 0;
  2358.     while (--j)
  2359.     {   if (fname[j] == '?')
  2360.             fname[j] = (k < fslen) ? fsnum[k++] : '0';
  2361.     }
  2362.  
  2363.     /* Open the file, write a FORM ILBM, and close it again */
  2364.  
  2365.     ifferr = 0;
  2366.     ifffptr = fopen(fname, "wb");
  2367.     if (ifffptr == NULL) ifferr = 1;
  2368.  
  2369.     addr1 = ifftell();
  2370.     iffputs("FORM");          /* FORM ILBM */
  2371.     iffputl(0);
  2372.     iffputs("ILBM");
  2373.  
  2374.     iffputs("BMHD");          /* BMHD */
  2375.     iffputl(20);
  2376.     iffputw(parm.page.xsize); /* Width */
  2377.     iffputw(parm.page.ysize); /* Height */
  2378.     iffputw(0);               /* X position */
  2379.     iffputw(0);               /* Y position */
  2380.     iffputb(parm.page.depth); /* Number of bit planes */
  2381.     iffputb(0);               /* Masking:     None */
  2382.     iffputb(1);               /* Compression: ByteRun */
  2383.     iffputb(0);               /* Pad1 */
  2384.     iffputw(0);               /* Transparent colour */
  2385.     iffputb(xa);              /* X aspect */
  2386.     iffputb(ya);              /* Y aspect */
  2387.     iffputw(parm.page.xsize); /* Source width */
  2388.     iffputw(parm.page.ysize); /* Source height */
  2389.  
  2390.     addr2 = ifftell();
  2391.     iffputs("CMAP");          /* CMAP */
  2392.     iffputl(0);
  2393.     for (i = 0; i < colormap.Count; i++)
  2394.     {   w = ((UWORD *) colormap.ColorTable)[i];
  2395.         iffputb(((w >> 8) & 15) << 4);
  2396.         iffputb(((w >> 4) & 15) << 4);
  2397.         iffputb(( w       & 15) << 4);
  2398.     }
  2399.     ifffixup(addr2);
  2400.  
  2401.     if (argscreen)
  2402.     {   iffputs("CAMG");      /* CAMG */
  2403.         iffputl(4);
  2404.         iffputl(newscreen.ViewModes);
  2405.     }
  2406.  
  2407.     addr2 = ifftell();
  2408.     iffputs("BODY");          /* BODY */
  2409.     iffputl(0);
  2410.     for (j = 0; j < bitmap.Rows; j++)
  2411.         for (i = 0; i < bitmap.Depth; i++)
  2412.             iffpack((char *) bitmap.Planes[i] + j * bitmap.BytesPerRow,
  2413.                     bitmap.BytesPerRow);
  2414.     ifffixup(addr2);
  2415.  
  2416.     ifffixup(addr1);
  2417.  
  2418.     if (fclose(ifffptr) == EOF)
  2419.         ifferr = 1;
  2420.     if (ifferr) ioerror = errioerror;
  2421. }
  2422.  
  2423. /* Find the greatest common divisor of two positive integers.  If one is
  2424.  * zero the result is the other */
  2425.  
  2426. int igcd(int n, int m)
  2427. {   unsigned int n1, m1, r;
  2428.     if      (n > m)
  2429.     {   n1 = n;
  2430.         m1 = m;
  2431.     }
  2432.     else if (m > n)
  2433.     {   n1 = m;
  2434.         m1 = n;
  2435.     }
  2436.     else
  2437.         return n;
  2438.     while (m1 != 0)
  2439.     {   r = n1 % m1;
  2440.         n1 = m1;
  2441.         m1 = r;
  2442.     }
  2443.     return (int) n1;
  2444. }
  2445.  
  2446. /* End of file "post.c" */
  2447.